1
- using KratosSelfService . Models ;
1
+ using KratosSelfService . Extensions ;
2
+ using KratosSelfService . Models ;
2
3
using KratosSelfService . Services ;
3
4
using Microsoft . AspNetCore . Mvc ;
4
5
using Ory . Hydra . Client . Model ;
@@ -48,7 +49,7 @@ public async Task<IActionResult> ConsentGet([FromQuery(Name = "consent_challenge
48
49
public async Task < IActionResult > ConsentPost (
49
50
[ FromForm ( Name = "consent_challenge" ) ] string challenge ,
50
51
[ FromForm ] bool remember ,
51
- [ FromForm ( Name = "grant_scope" ) ] List < string > grantScopes ,
52
+ [ FromForm ( Name = "grant_scope" ) ] List < string > grantScopes ,
52
53
[ FromForm ] string action )
53
54
{
54
55
if ( env . HydraAdminUrl == null ) return NotFound ( ) ;
@@ -78,7 +79,18 @@ public async Task<IActionResult> ConsentPost(
78
79
logger . LogDebug ( "Consent request was accepted by the user" ) ;
79
80
var consentRequest = await oAuth2Api . GetOAuth2ConsentRequestAsync ( challenge ) ;
80
81
81
- var session = new HydraAcceptOAuth2ConsentRequestSession ( ) ;
82
+ var kratosSession = HttpContext . GetSession ( ) ! ;
83
+ var kratosTraits = ( Dictionary < string , dynamic > ) kratosSession . Identity . Traits ;
84
+
85
+ // https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims
86
+ var idToken = new Dictionary < string , dynamic > ( ) ;
87
+
88
+ // TODO use configuration to dynamically map scopes to traits
89
+ if ( grantScopes . Contains ( "email" ) && kratosTraits [ "email" ] != null )
90
+ idToken [ "email" ] = kratosTraits [ "email" ] ;
91
+
92
+ // The session allows us to set session data for id and access tokens
93
+ var hydraSession = new HydraAcceptOAuth2ConsentRequestSession ( idToken : idToken ) ;
82
94
83
95
var acceptRequest = await oAuth2Api . AcceptOAuth2ConsentRequestAsync ( challenge ,
84
96
new HydraAcceptOAuth2ConsentRequest
@@ -91,7 +103,7 @@ public async Task<IActionResult> ConsentPost(
91
103
// can peak inside the code for some ideas, but be aware that all data is fake
92
104
// and this only exists to fake a login system which works in accordance to OpenID Connect.
93
105
// If that variable is not set, the session will be used as-is.
94
- Session = session ,
106
+ Session = hydraSession ,
95
107
// ORY Hydra checks if requested audiences are allowed by the client, so we can simply echo this.
96
108
GrantAccessTokenAudience = consentRequest . RequestedAccessTokenAudience ,
97
109
// This tells hydra to remember this consent request and allow the same client to request the same
0 commit comments