| [3a89adb] | 1 | from typing import List
|
|---|
| 2 |
|
|---|
| 3 | {{#authMethods}}
|
|---|
| 4 | {{#isOAuth}}
|
|---|
| 5 |
|
|---|
| 6 | def info_from_{{name}}(token):
|
|---|
| 7 | """
|
|---|
| 8 | Validate and decode token.
|
|---|
| 9 | Returned value will be passed in 'token_info' parameter of your operation function, if there is one.
|
|---|
| 10 | 'sub' or 'uid' will be set in 'user' parameter of your operation function, if there is one.
|
|---|
| 11 | 'scope' or 'scopes' will be passed to scope validation function.
|
|---|
| 12 |
|
|---|
| 13 | :param token Token provided by Authorization header
|
|---|
| 14 | :type token: str
|
|---|
| 15 | :return: Decoded token information or None if token is invalid
|
|---|
| 16 | :rtype: dict | None
|
|---|
| 17 | """
|
|---|
| 18 | return {'scopes': ['read:pets', 'write:pets'], 'uid': 'user_id'}
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | def validate_scope_{{name}}(required_scopes, token_scopes):
|
|---|
| 22 | """
|
|---|
| 23 | Validate required scopes are included in token scope
|
|---|
| 24 |
|
|---|
| 25 | :param required_scopes Required scope to access called API
|
|---|
| 26 | :type required_scopes: List[str]
|
|---|
| 27 | :param token_scopes Scope present in token
|
|---|
| 28 | :type token_scopes: List[str]
|
|---|
| 29 | :return: True if access to called API is allowed
|
|---|
| 30 | :rtype: bool
|
|---|
| 31 | """
|
|---|
| 32 | return set(required_scopes).issubset(set(token_scopes))
|
|---|
| 33 |
|
|---|
| 34 | {{/isOAuth}}
|
|---|
| 35 | {{#isApiKey}}
|
|---|
| 36 |
|
|---|
| 37 | def info_from_{{name}}(api_key, required_scopes):
|
|---|
| 38 | """
|
|---|
| 39 | Check and retrieve authentication information from api_key.
|
|---|
| 40 | Returned value will be passed in 'token_info' parameter of your operation function, if there is one.
|
|---|
| 41 | 'sub' or 'uid' will be set in 'user' parameter of your operation function, if there is one.
|
|---|
| 42 |
|
|---|
| 43 | :param api_key API key provided by Authorization header
|
|---|
| 44 | :type api_key: str
|
|---|
| 45 | :param required_scopes Always None. Used for other authentication method
|
|---|
| 46 | :type required_scopes: None
|
|---|
| 47 | :return: Information attached to provided api_key or None if api_key is invalid or does not allow access to called API
|
|---|
| 48 | :rtype: dict | None
|
|---|
| 49 | """
|
|---|
| 50 | return {'uid': 'user_id'}
|
|---|
| 51 |
|
|---|
| 52 | {{/isApiKey}}
|
|---|
| 53 | {{#isBasicBasic}}
|
|---|
| 54 |
|
|---|
| 55 | def info_from_{{name}}(username, password, required_scopes):
|
|---|
| 56 | """
|
|---|
| 57 | Check and retrieve authentication information from basic auth.
|
|---|
| 58 | Returned value will be passed in 'token_info' parameter of your operation function, if there is one.
|
|---|
| 59 | 'sub' or 'uid' will be set in 'user' parameter of your operation function, if there is one.
|
|---|
| 60 |
|
|---|
| 61 | :param username login provided by Authorization header
|
|---|
| 62 | :type username: str
|
|---|
| 63 | :param password password provided by Authorization header
|
|---|
| 64 | :type password: str
|
|---|
| 65 | :param required_scopes Always None. Used for other authentication method
|
|---|
| 66 | :type required_scopes: None
|
|---|
| 67 | :return: Information attached to user or None if credentials are invalid or does not allow access to called API
|
|---|
| 68 | :rtype: dict | None
|
|---|
| 69 | """
|
|---|
| 70 | return {'uid': 'user_id'}
|
|---|
| 71 |
|
|---|
| 72 | {{/isBasicBasic}}
|
|---|
| 73 | {{#isBasicBearer}}
|
|---|
| 74 |
|
|---|
| 75 | def info_from_{{name}}(token):
|
|---|
| 76 | """
|
|---|
| 77 | Check and retrieve authentication information from custom bearer token.
|
|---|
| 78 | Returned value will be passed in 'token_info' parameter of your operation function, if there is one.
|
|---|
| 79 | 'sub' or 'uid' will be set in 'user' parameter of your operation function, if there is one.
|
|---|
| 80 |
|
|---|
| 81 | :param token Token provided by Authorization header
|
|---|
| 82 | :type token: str
|
|---|
| 83 | :return: Decoded token information or None if token is invalid
|
|---|
| 84 | :rtype: dict | None
|
|---|
| 85 | """
|
|---|
| 86 | return {'uid': 'user_id'}
|
|---|
| 87 |
|
|---|
| 88 | {{/isBasicBearer}}
|
|---|
| 89 | {{/authMethods}}
|
|---|
| 90 |
|
|---|