19
19
)
20
20
21
21
22
+ def verify_jwt_in_request ():
23
+ """
24
+ Ensure that the requeste has a valid access token. This does not check the
25
+ freshness of the access token. Raises an appropiate exception there is
26
+ no token or if the token is invalid.
27
+ """
28
+ if request .method not in config .exempt_methods :
29
+ jwt_data = _decode_jwt_from_request (request_type = 'access' )
30
+ ctx_stack .top .jwt = jwt_data
31
+ verify_token_claims (jwt_data )
32
+ _load_user (jwt_data [config .identity_claim_key ])
33
+
34
+
35
+ def verify_jwt_in_request_optional ():
36
+ """
37
+ Optionally check if this request has a valid access token. If an access
38
+ token in present in the request, :func:`~flask_jwt_extended.get_jwt_identity`
39
+ will return the identity of the access token. If no access token is
40
+ present in the request, this simply returns, and
41
+ :func:`~flask_jwt_extended.get_jwt_identity` will return `None` instead.
42
+
43
+ If there is an invalid access token in the request (expired, tampered with,
44
+ etc), this will still raise the appropiate exception.
45
+ """
46
+ try :
47
+ if request .method not in config .exempt_methods :
48
+ jwt_data = _decode_jwt_from_request (request_type = 'access' )
49
+ ctx_stack .top .jwt = jwt_data
50
+ verify_token_claims (jwt_data )
51
+ _load_user (jwt_data [config .identity_claim_key ])
52
+ except (NoAuthorizationError , InvalidHeaderError ):
53
+ pass
54
+
55
+
56
+ def verify_fresh_jwt_in_request ():
57
+ """
58
+ Ensure that the requeste has a valid and fresh access token. Raises an
59
+ appropiate exception if there is no token, the token is invalid, or the
60
+ token is not marked as fresh.
61
+ """
62
+ if request .method not in config .exempt_methods :
63
+ jwt_data = _decode_jwt_from_request (request_type = 'access' )
64
+ ctx_stack .top .jwt = jwt_data
65
+ fresh = jwt_data ['fresh' ]
66
+ if isinstance (fresh , bool ):
67
+ if not fresh :
68
+ raise FreshTokenRequired ('Fresh token required' )
69
+ else :
70
+ now = timegm (datetime .utcnow ().utctimetuple ())
71
+ if fresh < now :
72
+ raise FreshTokenRequired ('Fresh token required' )
73
+ verify_token_claims (jwt_data )
74
+ _load_user (jwt_data [config .identity_claim_key ])
75
+
76
+
77
+ def verify_jwt_refresh_token_in_request ():
78
+ """
79
+ Ensure that the requeste has a valid refresh token. Raises an appropiate
80
+ exception if there is no token or the token is invalid.
81
+ """
82
+ if request .method not in config .exempt_methods :
83
+ jwt_data = _decode_jwt_from_request (request_type = 'refresh' )
84
+ ctx_stack .top .jwt = jwt_data
85
+ _load_user (jwt_data [config .identity_claim_key ])
86
+
87
+
22
88
def jwt_required (fn ):
23
89
"""
24
90
A decorator to protect a Flask endpoint.
@@ -31,11 +97,7 @@ def jwt_required(fn):
31
97
"""
32
98
@wraps (fn )
33
99
def wrapper (* args , ** kwargs ):
34
- if request .method not in config .exempt_methods :
35
- jwt_data = _decode_jwt_from_request (request_type = 'access' )
36
- ctx_stack .top .jwt = jwt_data
37
- verify_token_claims (jwt_data )
38
- _load_user (jwt_data [config .identity_claim_key ])
100
+ verify_jwt_in_request ()
39
101
return fn (* args , ** kwargs )
40
102
return wrapper
41
103
@@ -56,13 +118,7 @@ def jwt_optional(fn):
56
118
"""
57
119
@wraps (fn )
58
120
def wrapper (* args , ** kwargs ):
59
- try :
60
- jwt_data = _decode_jwt_from_request (request_type = 'access' )
61
- ctx_stack .top .jwt = jwt_data
62
- verify_token_claims (jwt_data )
63
- _load_user (jwt_data [config .identity_claim_key ])
64
- except (NoAuthorizationError , InvalidHeaderError ):
65
- pass
121
+ verify_jwt_in_request_optional ()
66
122
return fn (* args , ** kwargs )
67
123
return wrapper
68
124
@@ -79,19 +135,7 @@ def fresh_jwt_required(fn):
79
135
"""
80
136
@wraps (fn )
81
137
def wrapper (* args , ** kwargs ):
82
- if request .method not in config .exempt_methods :
83
- jwt_data = _decode_jwt_from_request (request_type = 'access' )
84
- ctx_stack .top .jwt = jwt_data
85
- fresh = jwt_data ['fresh' ]
86
- if isinstance (fresh , bool ):
87
- if not fresh :
88
- raise FreshTokenRequired ('Fresh token required' )
89
- else :
90
- now = timegm (datetime .utcnow ().utctimetuple ())
91
- if fresh < now :
92
- raise FreshTokenRequired ('Fresh token required' )
93
- verify_token_claims (jwt_data )
94
- _load_user (jwt_data [config .identity_claim_key ])
138
+ verify_fresh_jwt_in_request ()
95
139
return fn (* args , ** kwargs )
96
140
return wrapper
97
141
@@ -105,10 +149,7 @@ def jwt_refresh_token_required(fn):
105
149
"""
106
150
@wraps (fn )
107
151
def wrapper (* args , ** kwargs ):
108
- if request .method not in config .exempt_methods :
109
- jwt_data = _decode_jwt_from_request (request_type = 'refresh' )
110
- ctx_stack .top .jwt = jwt_data
111
- _load_user (jwt_data [config .identity_claim_key ])
152
+ verify_jwt_refresh_token_in_request ()
112
153
return fn (* args , ** kwargs )
113
154
return wrapper
114
155
0 commit comments