18
18
from sentry .models .apitoken import ApiToken
19
19
from sentry .sentry_apps .token_exchange .util import GrantTypes
20
20
from sentry .utils import json , metrics
21
+ from sentry .utils .locking import UnableToAcquireLock
21
22
from sentry .web .frontend .base import control_silo_view
22
23
from sentry .web .frontend .openidtoken import OpenIDToken
23
24
@@ -128,7 +129,9 @@ def post(self, request: Request) -> HttpResponse:
128
129
def get_access_tokens (self , request : Request , application : ApiApplication ) -> dict :
129
130
code = request .POST .get ("code" )
130
131
try :
131
- grant = ApiGrant .objects .get (application = application , code = code )
132
+ grant = ApiGrant .objects .get (
133
+ application = application , application__status = ApiApplicationStatus .active , code = code
134
+ )
132
135
except ApiGrant .DoesNotExist :
133
136
return {"error" : "invalid_grant" , "reason" : "invalid grant" }
134
137
@@ -141,7 +144,12 @@ def get_access_tokens(self, request: Request, application: ApiApplication) -> di
141
144
elif grant .redirect_uri != redirect_uri :
142
145
return {"error" : "invalid_grant" , "reason" : "invalid redirect URI" }
143
146
144
- token_data = {"token" : ApiToken .from_grant (grant = grant )}
147
+ try :
148
+ token_data = {"token" : ApiToken .from_grant (grant = grant )}
149
+ except UnableToAcquireLock :
150
+ # TODO(mdtro): we should return a 409 status code here
151
+ return {"error" : "invalid_grant" , "reason" : "invalid grant" }
152
+
145
153
if grant .has_scope ("openid" ) and options .get ("codecov.signing_secret" ):
146
154
open_id_token = OpenIDToken (
147
155
request .POST .get ("client_id" ),
@@ -150,6 +158,7 @@ def get_access_tokens(self, request: Request, application: ApiApplication) -> di
150
158
nonce = request .POST .get ("nonce" ),
151
159
)
152
160
token_data ["id_token" ] = open_id_token .get_signed_id_token (grant = grant )
161
+
153
162
return token_data
154
163
155
164
def get_refresh_token (self , request : Request , application : ApiApplication ) -> dict :
0 commit comments