authentik.providers.oauth2.views.device_finish
Device flow finish stage
1"""Device flow finish stage""" 2 3from django.http import HttpResponse 4from rest_framework.fields import CharField 5 6from authentik.events.models import Event, EventAction 7from authentik.flows.challenge import Challenge, ChallengeResponse 8from authentik.flows.planner import PLAN_CONTEXT_APPLICATION, FlowPlan 9from authentik.flows.stage import ChallengeStageView 10from authentik.flows.views.executor import SESSION_KEY_PLAN 11from authentik.providers.oauth2.models import DeviceToken 12 13PLAN_CONTEXT_DEVICE = "goauthentik.io/providers/oauth2/device" 14 15 16class OAuthDeviceCodeFinishChallenge(Challenge): 17 """Final challenge after user enters their code""" 18 19 component = CharField(default="ak-provider-oauth2-device-code-finish") 20 21 22class OAuthDeviceCodeFinishChallengeResponse(ChallengeResponse): 23 """Response that device has been authenticated and tab can be closed""" 24 25 component = CharField(default="ak-provider-oauth2-device-code-finish") 26 27 28class OAuthDeviceCodeFinishStage(ChallengeStageView): 29 """Stage to finish the OAuth device code flow""" 30 31 response_class = OAuthDeviceCodeFinishChallengeResponse 32 33 def get_challenge(self, *args, **kwargs) -> Challenge: 34 plan: FlowPlan = self.request.session[SESSION_KEY_PLAN] 35 token: DeviceToken = plan.context[PLAN_CONTEXT_DEVICE] 36 # As we're required to be authenticated by now, we can rely on 37 # request.user 38 token.user = self.request.user 39 token.session = self.request.session.get("authenticatedsession") 40 token.save() 41 # Log Application Authorization 42 Event.new( 43 EventAction.AUTHORIZE_APPLICATION, 44 authorized_application=plan.context[PLAN_CONTEXT_APPLICATION], 45 flow=plan.flow_pk, 46 scopes=" ".join(token.scope), 47 ).from_http(self.request) 48 return OAuthDeviceCodeFinishChallenge( 49 data={ 50 "component": "ak-provider-oauth2-device-code-finish", 51 } 52 ) 53 54 def challenge_valid(self, response: ChallengeResponse) -> HttpResponse: 55 return self.executor.stage_ok()
PLAN_CONTEXT_DEVICE =
'goauthentik.io/providers/oauth2/device'
17class OAuthDeviceCodeFinishChallenge(Challenge): 18 """Final challenge after user enters their code""" 19 20 component = CharField(default="ak-provider-oauth2-device-code-finish")
Final challenge after user enters their code
23class OAuthDeviceCodeFinishChallengeResponse(ChallengeResponse): 24 """Response that device has been authenticated and tab can be closed""" 25 26 component = CharField(default="ak-provider-oauth2-device-code-finish")
Response that device has been authenticated and tab can be closed
29class OAuthDeviceCodeFinishStage(ChallengeStageView): 30 """Stage to finish the OAuth device code flow""" 31 32 response_class = OAuthDeviceCodeFinishChallengeResponse 33 34 def get_challenge(self, *args, **kwargs) -> Challenge: 35 plan: FlowPlan = self.request.session[SESSION_KEY_PLAN] 36 token: DeviceToken = plan.context[PLAN_CONTEXT_DEVICE] 37 # As we're required to be authenticated by now, we can rely on 38 # request.user 39 token.user = self.request.user 40 token.session = self.request.session.get("authenticatedsession") 41 token.save() 42 # Log Application Authorization 43 Event.new( 44 EventAction.AUTHORIZE_APPLICATION, 45 authorized_application=plan.context[PLAN_CONTEXT_APPLICATION], 46 flow=plan.flow_pk, 47 scopes=" ".join(token.scope), 48 ).from_http(self.request) 49 return OAuthDeviceCodeFinishChallenge( 50 data={ 51 "component": "ak-provider-oauth2-device-code-finish", 52 } 53 ) 54 55 def challenge_valid(self, response: ChallengeResponse) -> HttpResponse: 56 return self.executor.stage_ok()
Stage to finish the OAuth device code flow
response_class =
<class 'OAuthDeviceCodeFinishChallengeResponse'>
34 def get_challenge(self, *args, **kwargs) -> Challenge: 35 plan: FlowPlan = self.request.session[SESSION_KEY_PLAN] 36 token: DeviceToken = plan.context[PLAN_CONTEXT_DEVICE] 37 # As we're required to be authenticated by now, we can rely on 38 # request.user 39 token.user = self.request.user 40 token.session = self.request.session.get("authenticatedsession") 41 token.save() 42 # Log Application Authorization 43 Event.new( 44 EventAction.AUTHORIZE_APPLICATION, 45 authorized_application=plan.context[PLAN_CONTEXT_APPLICATION], 46 flow=plan.flow_pk, 47 scopes=" ".join(token.scope), 48 ).from_http(self.request) 49 return OAuthDeviceCodeFinishChallenge( 50 data={ 51 "component": "ak-provider-oauth2-device-code-finish", 52 } 53 )
Return the challenge that the client should solve
def
challenge_valid( self, response: authentik.flows.challenge.ChallengeResponse) -> django.http.response.HttpResponse:
55 def challenge_valid(self, response: ChallengeResponse) -> HttpResponse: 56 return self.executor.stage_ok()
Callback when the challenge has the correct format