22// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33using CaseManagement . BPMN . Domains ;
44using CaseManagement . BPMN . Host . Delegates ;
5+ using MassTransit ;
56using Microsoft . AspNetCore . Authentication . JwtBearer ;
67using Microsoft . AspNetCore . Builder ;
78using Microsoft . AspNetCore . Hosting ;
1112using Microsoft . Extensions . Logging ;
1213using Microsoft . IdentityModel . Tokens ;
1314using Newtonsoft . Json ;
15+ using Newtonsoft . Json . Linq ;
1416using System ;
1517using System . Collections . Concurrent ;
1618using System . Collections . Generic ;
1719using System . IO ;
1820using System . Linq ;
21+ using System . Net . Http ;
22+ using System . Security . Claims ;
1923using System . Security . Cryptography ;
2024
2125namespace CaseManagement . BPMN . Host
2226{
2327 public class Startup
2428 {
29+ private Dictionary < string , string > MAPPING_OPENIDCLAIM_TO_CLAIM = new Dictionary < string , string >
30+ {
31+ { "sub" , ClaimTypes . NameIdentifier } ,
32+ { "role" , ClaimTypes . Role }
33+ } ;
2534 private readonly IHostingEnvironment _env ;
2635 private readonly IConfiguration _configuration ;
2736
@@ -44,11 +53,58 @@ public void ConfigureServices(IServiceCollection services)
4453 } )
4554 . AddJwtBearer ( options =>
4655 {
56+ options . Events = new JwtBearerEvents
57+ {
58+ OnTokenValidated = async ( ctx ) =>
59+ {
60+ var issuer = ctx . Principal . Claims . First ( c => c . Type == "iss" ) . Value ;
61+ using ( var httpClient = new HttpClient ( ) )
62+ {
63+ var authorization = ctx . Request . Headers [ "Authorization" ] [ 0 ] ;
64+ var bearer = authorization . Split ( " " ) . Last ( ) ;
65+ var requestMessage = new HttpRequestMessage
66+ {
67+ RequestUri = new Uri ( $ "{ issuer } /userinfo") ,
68+ Method = HttpMethod . Get
69+ } ;
70+ requestMessage . Headers . Add ( "Authorization" , $ "Bearer { bearer } ") ;
71+ var httpResponse = await httpClient . SendAsync ( requestMessage ) ;
72+ var json = await httpResponse . Content . ReadAsStringAsync ( ) ;
73+ var jObj = JObject . Parse ( json ) ;
74+ var identity = new ClaimsIdentity ( "userInfo" ) ;
75+ foreach ( var kvp in jObj )
76+ {
77+ var key = kvp . Key ;
78+ if ( MAPPING_OPENIDCLAIM_TO_CLAIM . ContainsKey ( key ) )
79+ {
80+ key = MAPPING_OPENIDCLAIM_TO_CLAIM [ key ] ;
81+ }
82+
83+ if ( kvp . Value . ToString ( ) . StartsWith ( '[' ) )
84+ {
85+ var arr = JArray . Parse ( kvp . Value . ToString ( ) ) . Select ( _ => _ . ToString ( ) ) . ToList ( ) ;
86+ foreach ( var str in arr )
87+ {
88+ identity . AddClaim ( new Claim ( kvp . Key , str ) ) ;
89+ }
90+ }
91+ else
92+ {
93+ identity . AddClaim ( new Claim ( kvp . Key , kvp . Value . ToString ( ) ) ) ;
94+ }
95+ }
96+
97+ var principal = new ClaimsPrincipal ( identity ) ;
98+ ctx . Principal = principal ;
99+ }
100+ }
101+ } ;
47102 options . TokenValidationParameters = new TokenValidationParameters
48103 {
49104 IssuerSigningKey = ExtractKey ( "openid_puk.txt" ) ,
50105 ValidAudiences = new List < string >
51106 {
107+ "caseManagementWebsite" ,
52108 "https://localhost:60000" ,
53109 "https://simpleidserver.northeurope.cloudapp.azure.com/openid"
54110 } ,
@@ -69,6 +125,7 @@ public void ConfigureServices(IServiceCollection services)
69125 opts . CallbackUrl = "http://localhost:60007/processinstances/{id}/complete/{eltId}" ;
70126 } ) . AddProcessFiles ( files ) . AddDelegateConfigurations ( GetDelegateConfigurations ( ) ) ;
71127 services . AddSwaggerGen ( ) ;
128+ services . AddMassTransitHostedService ( ) ;
72129 services . Configure < ForwardedHeadersOptions > ( options =>
73130 {
74131 options . ForwardedHeaders = ForwardedHeaders . XForwardedFor | ForwardedHeaders . XForwardedProto ;
@@ -131,6 +188,7 @@ private static byte[] Base64DecodeBytes(string base64EncodedData)
131188
132189 private static ConcurrentBag < DelegateConfigurationAggregate > GetDelegateConfigurations ( )
133190 {
191+ var credential = JsonConvert . DeserializeObject < CredentialsParameter > ( File . ReadAllText ( Path . Combine ( Directory . GetCurrentDirectory ( ) , "credentials.json" ) ) ) ;
134192 var getWeatherInformationDelegate = DelegateConfigurationAggregate . Create ( "GetWeatherInformationDelegate" , typeof ( GetWeatherInformationDelegate ) . FullName ) ;
135193 getWeatherInformationDelegate . AddDisplayName ( "fr" , "Récupérer météo" ) ;
136194 getWeatherInformationDelegate . AddDescription ( "fr" , "Récupérer les informations sur la météo" ) ;
@@ -142,21 +200,21 @@ private static ConcurrentBag<DelegateConfigurationAggregate> GetDelegateConfigur
142200 sendEmailDelegate . AddDisplayName ( "en" , "Send email" ) ;
143201 sendEmailDelegate . AddRecord ( "httpBody" , "Please click on this link to update the password" ) ;
144202 sendEmailDelegate . AddRecord ( "subject" , "Update password" ) ;
145- sendEmailDelegate . AddRecord ( "fromEmail" , "" ) ;
146- sendEmailDelegate . AddRecord ( "smtpHost" , "" ) ;
147- sendEmailDelegate . AddRecord ( "smtpPort" , "" ) ;
148- sendEmailDelegate . AddRecord ( "smtpUserName" , "" ) ;
149- sendEmailDelegate . AddRecord ( "smtpPassword" , "" ) ;
150- sendEmailDelegate . AddRecord ( "smtpEnableSsl" , "" ) ;
203+ sendEmailDelegate . AddRecord ( "fromEmail" , credential . Login ) ;
204+ sendEmailDelegate . AddRecord ( "smtpHost" , "smtp.gmail.com " ) ;
205+ sendEmailDelegate . AddRecord ( "smtpPort" , "587 " ) ;
206+ sendEmailDelegate . AddRecord ( "smtpUserName" , credential . Login ) ;
207+ sendEmailDelegate . AddRecord ( "smtpPassword" , credential . Password ) ;
208+ sendEmailDelegate . AddRecord ( "smtpEnableSsl" , "true " ) ;
151209
152210 var updateUserPasswordDelegate = DelegateConfigurationAggregate . Create ( "UpdateUserPasswordDelegate" , typeof ( UpdateUserPasswordDelegate ) . FullName ) ;
153211 updateUserPasswordDelegate . AddDisplayName ( "fr" , "Mettre à jour le mot de passe" ) ;
154212 updateUserPasswordDelegate . AddDisplayName ( "en" , "Update password" ) ;
155- updateUserPasswordDelegate . AddRecord ( "clientId" , "" ) ;
156- updateUserPasswordDelegate . AddRecord ( "clientSecret" , "" ) ;
213+ updateUserPasswordDelegate . AddRecord ( "clientId" , "humanTaskClient " ) ;
214+ updateUserPasswordDelegate . AddRecord ( "clientSecret" , "humanTaskClientSecret " ) ;
157215 updateUserPasswordDelegate . AddRecord ( "tokenUrl" , "https://localhost:60000/token" ) ;
158- updateUserPasswordDelegate . AddRecord ( "userUrl" , "" ) ;
159- updateUserPasswordDelegate . AddRecord ( "scope" , "update_password " ) ;
216+ updateUserPasswordDelegate . AddRecord ( "userUrl" , "https://localhost:60000/management/users/{id}/password " ) ;
217+ updateUserPasswordDelegate . AddRecord ( "scope" , "manage_users " ) ;
160218
161219 return new ConcurrentBag < DelegateConfigurationAggregate >
162220 {
@@ -165,5 +223,11 @@ private static ConcurrentBag<DelegateConfigurationAggregate> GetDelegateConfigur
165223 updateUserPasswordDelegate
166224 } ;
167225 }
226+
227+ private class CredentialsParameter
228+ {
229+ public string Login { get ; set ; }
230+ public string Password { get ; set ; }
231+ }
168232 }
169233}
0 commit comments