Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Djangoのパスワードハッシュアルゴリズムで_PyramidのWebアプリケーション作った.pdf
Search
mizzsugar
May 19, 2020
Programming
980
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Djangoのパスワードハッシュアルゴリズムで_PyramidのWebアプリケーション作った.pdf
mizzsugar
May 19, 2020
More Decks by mizzsugar
See All by mizzsugar
自分のPCにPython環境を用意して_VSCodeでStreamlitアプリを動かすまでの8ステップ.pdf
mizzsugar
0
110
Colabのみで初心者向けPythonハンズオン.pdf
mizzsugar
0
91
厳しさとゆるさの間で迷う人に捧げる個人開発記
mizzsugar
0
76
SQLModel入門〜クエリと型〜
mizzsugar
3
1.6k
フルリモート向いてないと思っていた私が、なんだかんだ健やかに 1年半フルリモート出来ている話
mizzsugar
1
170
Djangoでのプロジェクトだって型ヒントを運用出来る!
mizzsugar
4
9.2k
「動くものは作れる」の一歩先へ 〜「自走プログラマー」の紹介〜
mizzsugar
0
660
pytestの第一歩 〜「テスト駆動Python」の紹介〜
mizzsugar
3
490
データ分析ツール開発でpoetryを使う選択肢
mizzsugar
1
1.2k
Other Decks in Programming
See All in Programming
FreeBSDでZabbixを動かす
kenkino
0
310
速く作れる。その次は、速く確かめられる開発へ 〜AIネイティブ開発を支える、Shift Down〜 / Can build fast. Next, moving to development where we can verify fast.
rkaga
5
3.4k
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
210
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
470
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
1
290
AIは賢い。でも実行環境は? CLIおじさんがAI時代に伝えたいこと ~ CLIおじさんがAI時代に伝えたいこと ~
curekoshimizu
1
250
The Rails Doctrine Decade
koic
2
140
SREの越境 / SRE Collaboration
y0hgi
2
250
Kiroで創り、AgentCoreで繋ぐ!AWSで実践する「AI-DLC」から「AIエージェント統合」までの最新地図
licux
4
700
{ Android | Kotlin } Gradle Plugin in 2026
ryunen344
1
320
Building an Out-of-Order CPU
latte72
1
790
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
210
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
135
10k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1k
How to build a perfect <img>
jonoalderson
1
6k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
550
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.9k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Producing Creativity
orderedlist
PRO
348
41k
Writing Fast Ruby
sferik
630
63k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.6k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.2k
Transcript
Djangoのパスワードハッシュア ルゴリズムで PyramidのWebアプリケーショ ン作った 2020-05-18 mizzsugar0425 @BPLL
お前誰よ • みずきと申します。 • Twitter @mizzsugar0425 • PythonでWebサービス開発してます。(本業Django, 副業Pyramid) •
PyConJPのCfPの締切に焦ってます(;´Д`)
今日話す話の対象となる人 • ある程度Djangoを使ったWebアプリケーションまたはサービスの開発をできる 人
今日話すこと • Djangoデフォルトパスワードアルゴリズムの説明(ざっくり) • Djangoのソースコードを一部適用させたのでライセンスの話 • 作ったアプリでどうやってパスワードアルゴリズムを適用させたか • 爆誕させたもの↓ https://github.com/mizzsugar/pyramid-app-with-django-password-hash
きっかけ
最近思うこと
Djangoは制約が多すぎて辛い
そうだ、リプレースすればいいんだ(雑)
いろいろやらないといけないこ とあるけど・・・ migrationファイルどーするの ログインユーザーのトークンとか ORMで発行したクエリ その他Djangoにまかせた何か
Djangoデフォルトの パスワードハッシュアルゴリズムを 他のWebフレームワークで使うには どうすれば?
Djangoのパスワードの仕組みたどってみた from django.contrib.auth.models import User user = User.objects.create(email='sample.example.com') user.set_password('password') #
パスワードハッシュ user.save() # return True user.check_password('password') # パスワードチェック
check_password, set_passwordが エントリポイントらしいぞ
たどったら django.contrib.auth.hashersモジュールの check_password(上), make_password(下) にたどり着いた。 https://github.com/django/django/blob/master/django/contrib/auth/hashers. py#L30 https://github.com/django/django/blob/master/django/contrib/auth/hashers. py#L64
色んなハッシュアルゴリズム使えるよう settings.pyのPASSWORD_HASHERSで使いたいパスワードアルゴリズムを定義 PASSWORD_HASHERS = [ 'django.contrib.auth.hashers.PBKDF2PasswordHasher', 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher', 'django.contrib.auth.hashers.Argon2PasswordHasher', 'django.contrib.auth.hashers.BCryptSHA256PasswordHasher', ]
デフォルトのsettings.pyのPASSWORD_HASHERS https://docs.djangoproject.com/ja/3.0/topics/auth/passwords/#how-django-stores-passwords
デフォルトではどのアルゴリズムが? PASSWORD_HASHERS = [ 'django.contrib.auth.hashers.PBKDF2PasswordHasher', 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher', 'django.contrib.auth.hashers.Argon2PasswordHasher', 'django.contrib.auth.hashers.BCryptSHA256PasswordHasher', ] このリストを読み込んで一番上のアルゴリズムのクラスを使う仕組み。
デフォルトだとdjango.contrib.auth.hashersモジュールの PBKDF2PassswordHasherクラスを使うよう。
これを適用するには? PBKDF2PassswordHasherクラスをコピペするのが楽だけど ライセンス大丈夫?
Djangoは3-clause BSD-3-Clauseライセンス 以下の条件下なら修正あり、なしに関わらずソースコードの再配布OK 1. ソースコードの再配布は、上記の著作権表示、ここに列挙された条件、および下記の免責条項を保 持すること。 2. バイナリ形式の再配布は、上記の著作権表示、ここに列挙された条件、および下記の免責条項は、ド キュメントまたは他の資料で配布すること。 3.
このソフトウェアのコントリビューター (貢献者)の名前は、特定の書面による事前の許可なしに、このソ フトウェアから派生した製品の保証または販売促進のために使用してはいけない。 https://opensource.org/licenses/BSD-3-Clause
リポジトリにDjangoライセンス明記 https://github.com/mizzsugar/pyramid-app-with-django-password-hash/blob/ master/LICENSE.Django
今回作ったリポジトリ自体のライセンス • BSD-3-Clauseにした • Django自身のライセンスよりもゆるくするのは怖いのでやめといた
今回作ったもののディレクトリ構成 • Djangoから取り入れた部分はビジネスロジックとは切り離してライブラリとして導 入する方針 • ビジネスロジックではmake_password, check_passwordを呼び出すだけにす る
├── LICENSE ├── LICENSE.Django ├── README.rst └── application ├── Makefile
├── migrations │ └── 0001_core.sql ├── mypy.ini ├── openapi.yaml ├── poetry.lock ├── sample │ ├── __init__.py │ ├── application.py │ ├── bootstrap.py │ ├── domain │ │ ├── __init__.py │ │ └── authentication.py(viewで呼び出す認証処理書いてる ) │ │ │ ├── libs │ │ ├── __init__.py │ │ ├── crypto.py(make_password, check_passwordに必要) │ │ ├── encoding.py(make_password, check_passwordに必要) │ │ ├── module_loading.py(make_password, check_passwordに必要) │ │ └── password.py(ここにmake_password, check_passwordがある) │ ├── repository │ ├── scripts │ ├── views │ └── wsgi.py └── tests ※一部省略しています。
パスワードチェックの実装 def sign_in(self, draft: sample.dto.SignIn) -> sample.domain.dto.User: try: user =
self._repository.authentication.fetch_by_email(draft.email) # DBアクセス except sample.repository.exceptions.NotFoundError: raise sample.domain.exceptions.InvalidCredentialError() if not sample.libs.password.check_password(draft.password, user.password): raise sample.domain.exceptions.InvalidCredentialError() return sample.domain.converters.User.from_repository(user)
パスワードを暗号化する実装 def register_user(self, draft: sample.domain.dto.DraftUser) -> None: password = sample.libs.password.make_password(draft.password)
# パスワードハッシュ try: self._repository.authentication.register_user( sample.repository.dto.DraftUser(email=draft.email, password=password)) # DBに登録 except sample.repository.exceptions.ConflictError: raise sample.domain.exceptions.AlreadyRegisteredError()
これからしたいこと • Djangoから得た部分のTypeHint • CIの設定 • パスワードのところもうちょっとテスト追加 • iterationの回数の増加タイミング見極め •
ログインした際のトークン発行
ありがとうございました。