-
Notifications
You must be signed in to change notification settings - Fork 14
Jwt #79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Jwt #79
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
3581516
Logic for jwt token
a001a9c
Adding jwt support
cd57e23
Cassette
48cf384
Testing
bc00876
Fix date
3b5bfc9
Cassettes
0ece32a
Final fixes
515818f
This is not neede
5c5d42c
Fix test
76e4cca
Upgrade version
3ecb36f
Pre release
79529da
Otro
5e3c112
Correct version
1f6aaf5
Version
cc13a5f
Default false
fa6c551
Create jwt class
a881408
README clarification
86c2051
Update README.md
ricardo8990 fd6fa40
Update README.md
ricardo8990 5dc849c
Fix comment
3e53411
Add type hint
837580b
Fix auth
64506c5
Add ignore in config
aed0a32
Rebase
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import base64 | ||
| import binascii | ||
| import datetime as dt | ||
| import json | ||
| from dataclasses import dataclass | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from .exc import MalformedJwtToken | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .http import Session | ||
|
|
||
|
|
||
| @dataclass | ||
| class Jwt: | ||
| expires_at: dt.datetime | ||
| token: str | ||
|
|
||
| @property | ||
| def is_expired(self) -> bool: | ||
| return self.expires_at - dt.datetime.utcnow() <= dt.timedelta( | ||
| minutes=5 | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def get_expiration_date(token: str) -> dt.datetime: | ||
| """ | ||
| Jwt tokens contains the exp field in the payload data, | ||
| this function extracts the date so we can validate the | ||
| token before any request | ||
| More info about JWT tokens at: https://jwt.io/ | ||
| """ | ||
| try: | ||
| payload_encoded = token.split('.')[1] | ||
| payload = json.loads(base64.b64decode(f'{payload_encoded}==')) | ||
| except (IndexError, json.JSONDecodeError, binascii.Error): | ||
| raise MalformedJwtToken(f'Invalid JWT: {token}') | ||
| # Expiration timestamp can be found in the `exp` key in the payload | ||
| exp_timestamp = payload['exp'] | ||
| return dt.datetime.utcfromtimestamp(exp_timestamp) | ||
|
|
||
| @classmethod | ||
| def create(cls, session: 'Session') -> 'Jwt': | ||
| session.jwt_token = None | ||
| token = session.post('/token', dict())['token'] | ||
| expires_at = Jwt.get_expiration_date(token) | ||
| return cls(expires_at, token) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| __version__ = '0.4.2' | ||
| __version__ = '0.5.0' | ||
| CLIENT_VERSION = __version__ | ||
| API_VERSION = '2020-03-19' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| requests==2.25.0 | ||
| cuenca-validations==0.6.9 | ||
| dataclasses>=0.7;python_version<"3.7" | ||
| aws-requests-auth==0.4.3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| interactions: | ||
| - request: | ||
| body: null | ||
| headers: | ||
| Accept: | ||
| - '*/*' | ||
| Accept-Encoding: | ||
| - gzip, deflate | ||
| Authorization: | ||
| - DUMMY | ||
| Connection: | ||
| - keep-alive | ||
| Content-Length: | ||
| - '0' | ||
| User-Agent: | ||
| - cuenca-python/0.3.6 | ||
| X-Cuenca-Api-Version: | ||
| - '2020-03-19' | ||
| method: POST | ||
| uri: https://api.cuenca.com/token | ||
| response: | ||
| body: | ||
| string: '{"id":1,"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MDM4NDM2NTUsImlhdCI6MTYwMzIzODg1NSwic3ViIjoiQUtib3hCeXNTd1NTbVR0VmtoT1I2X21RIn0.VAT9w746K0hfOKnvZCFxwBDKjV71j-ItaFdZrIc0xCM","created_at":"2020-10-21T00:07:35.290968+00:00","api_key_id":"AKboxBysSwSSmTtVkhOR6_mQ"}' | ||
| headers: | ||
| Connection: | ||
| - keep-alive | ||
| Content-Length: | ||
| - '279' | ||
| Content-Type: | ||
| - application/json | ||
| Date: | ||
| - Wed, 21 Oct 2020 00:07:35 GMT | ||
| Server: | ||
| - nginx/1.18.0 | ||
| X-Amzn-Trace-Id: | ||
| - Root=1-5f8f7bc6-16318c2505b404c030bbcaf4;Sampled=0 | ||
| x-amz-apigw-id: | ||
| - UvBHAHOGCYcFZTw= | ||
| x-amzn-RequestId: | ||
| - 856c8bef-f148-48e4-a27b-391749b6b9f6 | ||
| status: | ||
| code: 201 | ||
| message: Created | ||
| version: 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| interactions: | ||
| - request: | ||
| body: null | ||
| headers: | ||
| Accept: | ||
| - '*/*' | ||
| Accept-Encoding: | ||
| - gzip, deflate | ||
| Authorization: | ||
| - DUMMY | ||
| Connection: | ||
| - keep-alive | ||
| Content-Length: | ||
| - '0' | ||
| User-Agent: | ||
| - cuenca-python/0.3.6 | ||
| X-Cuenca-Api-Version: | ||
| - '2020-03-19' | ||
| method: POST | ||
| uri: https://api.cuenca.com/token | ||
| response: | ||
| body: | ||
| string: '{"id":12,"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MDM5MTA5NDksImlhdCI6MTYwMzMwNjE0OSwic3ViIjoiQUtib3hCeXNTd1NTbVR0VmtoT1I2X21RIiwidWlkIjoiMWFkNWUwZTMtMTNjZS0xMWViLTg0MjItNDE3MmZhYTQwZjQyIn0.5OX-MCcgi4cyiGexSsDIwk8HyhJxuIT8VF7X41GkO6w","created_at":"2020-10-21T18:49:09.935845","api_key":{"id":"AKboxBysSwSSmTtVkhOR6_mQ","created_at":"2020-10-20T23:56:28.845757","secret":"********","deactivated_at":null,"user_id":"rsc"}}' | ||
| headers: | ||
| Connection: | ||
| - keep-alive | ||
| Content-Length: | ||
| - '438' | ||
| Content-Type: | ||
| - application/json | ||
| Date: | ||
| - Wed, 21 Oct 2020 18:49:09 GMT | ||
| Server: | ||
| - nginx/1.18.0 | ||
| X-Amzn-Trace-Id: | ||
| - Root=1-5f9082a5-37ce7b1275ae2055215eb14d;Sampled=0 | ||
| x-amz-apigw-id: | ||
| - UxlZ8FEyCYcFUuQ= | ||
| x-amzn-RequestId: | ||
| - ee534c24-2878-4efe-b91e-3c99757adff6 | ||
| status: | ||
| code: 201 | ||
| message: Created | ||
| - request: | ||
| body: null | ||
| headers: | ||
| Accept: | ||
| - '*/*' | ||
| Accept-Encoding: | ||
| - gzip, deflate | ||
| Authorization: | ||
| - DUMMY | ||
| Connection: | ||
| - keep-alive | ||
| Content-Length: | ||
| - '0' | ||
| User-Agent: | ||
| - cuenca-python/0.3.6 | ||
| X-Cuenca-Api-Version: | ||
| - '2020-03-19' | ||
| X-Cuenca-Token: | ||
| - DUMMY | ||
| method: POST | ||
| uri: https://api.cuenca.com/token | ||
| response: | ||
| body: | ||
| string: '{"id":13,"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MDM5MTA5NTAsImlhdCI6MTYwMzMwNjE1MCwic3ViIjoiQUtib3hCeXNTd1NTbVR0VmtoT1I2X21RIiwidWlkIjoiMWFlMjQ5MjUtMTNjZS0xMWViLTk0ZDQtNDE3MmZhYTQwZjQyIn0.GmDxVfvt2Es6mf1upY5Y7RdkGgeos7mTpA_7wFb8ZaI","created_at":"2020-10-21T18:49:10.017165","api_key":{"id":"AKboxBysSwSSmTtVkhOR6_mQ","created_at":"2020-10-20T23:56:28.845757","secret":"********","deactivated_at":null,"user_id":"rsc"}}' | ||
| headers: | ||
| Connection: | ||
| - keep-alive | ||
| Content-Length: | ||
| - '438' | ||
| Content-Type: | ||
| - application/json | ||
| Date: | ||
| - Wed, 21 Oct 2020 18:49:10 GMT | ||
| Server: | ||
| - nginx/1.18.0 | ||
| X-Amzn-Trace-Id: | ||
| - Root=1-5f9082a5-25f2f46b7c8b558769ecf708;Sampled=0 | ||
| x-amz-apigw-id: | ||
| - UxlZ8Gs5CYcFtFg= | ||
| x-amzn-RequestId: | ||
| - 6553d156-d57e-48a4-b16a-42588c221827 | ||
| status: | ||
| code: 201 | ||
| message: Created | ||
| - request: | ||
| body: null | ||
| headers: | ||
| Accept: | ||
| - '*/*' | ||
| Accept-Encoding: | ||
| - gzip, deflate | ||
| Connection: | ||
| - keep-alive | ||
| User-Agent: | ||
| - cuenca-python/0.3.6 | ||
| X-Cuenca-Api-Version: | ||
| - '2020-03-19' | ||
| X-Cuenca-Token: | ||
| - DUMMY | ||
| method: GET | ||
| uri: https://api.cuenca.com/api_keys | ||
| response: | ||
| body: | ||
| string: '{"items":[{"id":"AKboxBysSwSSmTtVkhOR6_mQ","created_at":"2020-10-20T23:56:28.845757","secret":"********","deactivated_at":null,"user_id":"rsc"}],"next_page_uri":null}' | ||
| headers: | ||
| Connection: | ||
| - keep-alive | ||
| Content-Length: | ||
| - '166' | ||
| Content-Type: | ||
| - application/json | ||
| Date: | ||
| - Wed, 21 Oct 2020 18:49:10 GMT | ||
| Server: | ||
| - nginx/1.18.0 | ||
| X-Amzn-Trace-Id: | ||
| - Root=1-5f9082a6-6a70a6d03f0698b549541398;Sampled=0 | ||
| x-amz-apigw-id: | ||
| - UxlZ9G4tiYcFmjQ= | ||
| x-amzn-RequestId: | ||
| - e3f54e5b-13ec-4cf8-83da-4f93f7bd9c54 | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| version: 1 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.