@@ -54,16 +54,16 @@ def _authenticate_basic_auth(self, request):
54
54
encoding = request .encoding or 'utf-8'
55
55
56
56
auth_string_decoded = base64 .b64decode (auth_string ).decode (encoding )
57
- client_id , client_secret = auth_string_decoded .split (':' , 1 )
57
+ client_id , client_secret = map ( unquote_plus , auth_string_decoded .split (':' , 1 ) )
58
58
59
- try :
60
- request .client = Application .objects .get (client_id = unquote_plus (client_id ),
61
- client_secret = unquote_plus (client_secret ))
62
- return True
63
-
64
- except Application .DoesNotExist :
65
- log .debug ("Failed basic auth: Application %s do not exists" % unquote_plus (client_id ))
59
+ if self ._load_application (client_id , request ) is None :
60
+ log .debug ("Failed basic auth: Application %s do not exists" % client_id )
61
+ return False
62
+ elif request .client .client_secret != client_secret :
63
+ log .debug ("Failed basic auth: wrong client secret %s" % client_secret )
66
64
return False
65
+ else :
66
+ return True
67
67
68
68
def _authenticate_request_body (self , request ):
69
69
"""
@@ -73,20 +73,21 @@ def _authenticate_request_body(self, request):
73
73
Remember that this method is NOT RECOMMENDED and SHOULD be limited to clients unable to
74
74
directly utilize the HTTP Basic authentication scheme. See rfc:`2.3.1` for more details.
75
75
"""
76
+ #TODO: check if oauthlib has already unquoted client_id and client_secret
76
77
client_id = request .client_id
77
78
client_secret = request .client_secret
78
79
79
80
if not client_id or not client_secret :
80
81
return False
81
82
82
- try :
83
- request .client = Application .objects .get (client_id = client_id ,
84
- client_secret = client_secret )
85
- return True
86
-
87
- except Application .DoesNotExist :
88
- log .debug ("Failed body authentication: Application %s do not exists" % client_id )
83
+ if self ._load_application (client_id , request ) is None :
84
+ log .debug ("Failed body auth: Application %s does not exists" % client_id )
89
85
return False
86
+ elif request .client .client_secret != client_secret :
87
+ log .debug ("Failed body auth: wrong client secret %s" % client_secret )
88
+ return False
89
+ else :
90
+ return True
90
91
91
92
def _load_application (self , client_id , request ):
92
93
"""
@@ -95,8 +96,10 @@ def _load_application(self, client_id, request):
95
96
"""
96
97
try :
97
98
request .client = request .client or Application .objects .get (client_id = client_id )
99
+ return request .client
98
100
except Application .DoesNotExist :
99
- log .debug ("Application %s do not exists" % client_id )
101
+ log .debug ("Failed body authentication: Application %s do not exists" % client_id )
102
+ return None
100
103
101
104
def client_authentication_required (self , request , * args , ** kwargs ):
102
105
"""
@@ -151,15 +154,10 @@ def authenticate_client_id(self, client_id, request, *args, **kwargs):
151
154
proceed only if the client exists and it's not of type 'Confidential'.
152
155
Also assign Application instance to request.client.
153
156
"""
154
-
155
- try :
156
- request .client = Application .objects .get (client_id = client_id )
157
+ if self ._load_application (client_id , request ) is not None :
157
158
log .debug ("Application %s has type %s" % (client_id , request .client .client_type ))
158
159
return request .client .client_type != Application .CLIENT_CONFIDENTIAL
159
-
160
- except Application .DoesNotExist :
161
- log .debug ("Application %s do not exists" % client_id )
162
- return False
160
+ return False
163
161
164
162
def confirm_redirect_uri (self , client_id , code , redirect_uri , client , * args , ** kwargs ):
165
163
"""
@@ -180,8 +178,7 @@ def validate_client_id(self, client_id, request, *args, **kwargs):
180
178
Ensure an Application exists with given client_id. If it exists, it's assigned to
181
179
request.client.
182
180
"""
183
- self ._load_application (client_id , request )
184
- return request .client is not None
181
+ return self ._load_application (client_id , request ) is not None
185
182
186
183
def get_default_redirect_uri (self , client_id , request , * args , ** kwargs ):
187
184
return request .client .default_redirect_uri
0 commit comments