python
[python][pyjwt] JWT example
kimxavi
2020. 2. 3. 17:42
반응형
PyJWT 를 사용해서 JWT(json web token) 구현해보자
설치
pip install pyjwt
알고리즘 'HS256', 'HS384', 'HS512' 를 쓸 경우엔 위와 같이 설치한다.
그냥 해보니
NotImplementedError: Algorithm 'RS256' could not be found. Do you have cryptography installed?
에러가 발생.. 소스 확인해보니,
알고리즘 'RS256', 'RS384', 'RS512', 'ES256', 'ES384', 'ES521', 'ES512', 'PS256', 'PS384', 'PS512'
를 쓸 경우엔 추가 설치 필요한 것 같다.
pip install pyjwt[crypto]
예제
RS256 알고리즘 사용할 예정
소스 코드
import time
import jwt
# Encoding
private_key = b'-----BEGIN PRIVATE KEY-----\nMIGEAgEAMBAGByqGSM49AgEGBS...'
now = int(time.time())
exp = now + 2000
jwt_payload = {'iss': 'ISS', 'iat': now, 'exp': exp}
encoded = jwt.encode(jwt_payload, key, 'RS256')
# Decoding
public_key = '...'
decoded = jwt.decode(encoded, public_key, algorithms='RS256')
JWT : ( https://jwt.io/introduction/ )
PyJWT : ( https://pyjwt.readthedocs.io/en/latest/usage.html#encoding-decoding-tokens-with-rs256-rsa )
반응형