9
9
using IdentityModel . AspNetCore . OAuth2Introspection ;
10
10
using IdentityServer4 . AccessTokenValidation ;
11
11
using IdentityServer4 . Services ;
12
+ using Microsoft . AspNetCore . Authentication . JwtBearer ;
13
+ using Microsoft . AspNetCore . Authorization ;
12
14
using Microsoft . AspNetCore . Builder ;
13
15
using Microsoft . AspNetCore . Identity ;
14
16
using Microsoft . Extensions . Configuration ;
15
17
using Microsoft . Extensions . DependencyInjection ;
16
18
using Microsoft . Extensions . Logging ;
17
19
using Microsoft . IdentityModel . Logging ;
20
+ using Newtonsoft . Json ;
21
+ using Newtonsoft . Json . Linq ;
18
22
using RawCMS . Library . Core ;
19
23
using RawCMS . Library . Core . Interfaces ;
24
+ using RawCMS . Library . Service ;
20
25
using RawCMS . Plugins . Core . Configuration ;
21
26
using RawCMS . Plugins . Core . Extensions ;
22
27
using RawCMS . Plugins . Core . Handlers ;
23
28
using RawCMS . Plugins . Core . Model ;
24
29
using RawCMS . Plugins . Core . Stores ;
30
+ using System ;
31
+ using System . Collections . Generic ;
32
+ using System . IdentityModel . Tokens . Jwt ;
33
+ using System . Linq ;
34
+ using System . Net . Http ;
35
+ using System . Net . Http . Headers ;
25
36
using System . Security . Claims ;
26
37
27
38
namespace RawCMS . Plugins . Core
@@ -45,7 +56,7 @@ public AuthPlugin(AppEngine appEngine, AuthConfig config, ILogger logger) : base
45
56
public override void ConfigureServices ( IServiceCollection services )
46
57
{
47
58
IdentityModelEventSource . ShowPII = true ;
48
-
59
+
49
60
// all singleton works on CI/docker env but not on local
50
61
//services.AddSingleton<IUserStore<IdentityUser>, RawUserStore>();
51
62
//services.AddSingleton<IUserPasswordStore<IdentityUser>, RawUserStore>();
@@ -54,7 +65,7 @@ public override void ConfigureServices(IServiceCollection services)
54
65
//services.AddSingleton<IPasswordHasher<IdentityUser>, RawUserStore>();
55
66
//services.AddSingleton<IProfileService, RawUserStore>();
56
67
//services.AddSingleton<IUserClaimsPrincipalFactory<IdentityUser>, RawClaimsFactory>();
57
-
68
+
58
69
// this works on local
59
70
services . AddScoped < IUserStore < IdentityUser > , RawUserStore > ( ) ;
60
71
services . AddScoped < IUserPasswordStore < IdentityUser > , RawUserStore > ( ) ;
@@ -63,8 +74,8 @@ public override void ConfigureServices(IServiceCollection services)
63
74
services . AddScoped < IPasswordHasher < IdentityUser > , RawUserStore > ( ) ;
64
75
services . AddScoped < IProfileService , RawUserStore > ( ) ;
65
76
services . AddScoped < IUserClaimsPrincipalFactory < IdentityUser > , RawClaimsFactory > ( ) ;
66
-
67
-
77
+
78
+
68
79
services . AddScoped < RawRoleStore > ( ) ;
69
80
services . AddScoped < IRoleStore < IdentityRole > , RawRoleStore > ( ) ;
70
81
services . AddIdentity < IdentityUser , IdentityRole > ( ) ;
@@ -79,68 +90,132 @@ public override void ConfigureServices(IServiceCollection services)
79
90
. AddAspNetIdentity < IdentityUser > ( )
80
91
. AddProfileServiceCustom ( ) ;
81
92
82
- if ( config . Mode == OAuthMode . External )
93
+ //if (config.Mode == OAuthMode.External)
94
+ //{
95
+ // OAuth2IntrospectionOptions options = new OAuth2IntrospectionOptions
96
+ // {
97
+ // //base - address of your identityserver
98
+ // Authority = config.Authority,
99
+ // ClientSecret = config.ClientSecret,
100
+ // ClientId = config.ClientId,
101
+ // BasicAuthenticationHeaderStyle = IdentityModel.Client.BasicAuthenticationHeaderStyle.Rfc2617
102
+ // };
103
+ // if (!string.IsNullOrWhiteSpace(config.IntrospectionEndpoint))
104
+ // {
105
+ // options.IntrospectionEndpoint = config.IntrospectionEndpoint;
106
+ // }
107
+ // options.TokenTypeHint = "Bearer";
108
+ // if (!string.IsNullOrWhiteSpace(config.TokenTypeHint))
109
+ // {
110
+ // options.TokenTypeHint = config.TokenTypeHint;
111
+ // }
112
+
113
+ // options.Validate();
114
+
115
+ // services.AddAuthentication(OAuth2IntrospectionDefaults.AuthenticationScheme)
116
+ // .AddOAuth2Introspection(x =>
117
+ // {
118
+ // x = options;
119
+ // });
120
+ //}
121
+ //else
122
+ //{
123
+
124
+ //}
125
+
126
+ var schemeList = new List < string > { "Bearer" } ;
127
+ var authBuilder = services . AddAuthentication ( ) ;
128
+
129
+ //rawcms providers
130
+ authBuilder = authBuilder . AddScheme < RawIdentityServerAuthenticationOptions , RawLocalAccessTokenValidationHandler > ( "Bearer" , ( o ) =>
131
+ {
132
+ o . AdminApiKey = this . config . RawCMSProvider . AdminApiKey ;
133
+ o . ApiKey = this . config . RawCMSProvider . ApiKey ;
134
+ } ) ;
135
+
136
+ if ( this . config . ExternalProvider != null && this . config . ExternalProvider . Count > 0 )
83
137
{
84
- OAuth2IntrospectionOptions options = new OAuth2IntrospectionOptions
85
- {
86
- //base - address of your identityserver
87
- Authority = config . Authority ,
88
- ClientSecret = config . ClientSecret ,
89
- ClientId = config . ClientId ,
90
- BasicAuthenticationHeaderStyle = IdentityModel . Client . BasicAuthenticationHeaderStyle . Rfc2617
91
- } ;
92
- if ( ! string . IsNullOrWhiteSpace ( config . IntrospectionEndpoint ) )
93
- {
94
- options . IntrospectionEndpoint = config . IntrospectionEndpoint ;
95
- }
96
- options . TokenTypeHint = "Bearer" ;
97
- if ( ! string . IsNullOrWhiteSpace ( config . TokenTypeHint ) )
138
+ var crudService = services . BuildServiceProvider ( ) . GetService < CRUDService > ( ) ;
139
+
140
+ foreach ( var provider in this . config . ExternalProvider )
98
141
{
99
- options . TokenTypeHint = config . TokenTypeHint ;
142
+ //TODO: add multiple authintication schema type
143
+ authBuilder = authBuilder . AddJwtProvider ( provider , crudService ) ;
144
+ schemeList . Add ( provider . SchemaName ) ;
100
145
}
146
+ }
101
147
102
- options . Validate ( ) ;
148
+
149
+ //.AddJwtBearer("Auth0Bis", x =>
150
+ //{
151
+ // x.Authority = "https://dev-t61xkx2b.eu.auth0.com";
152
+ // x.Audience = "http://localhost:1111";
153
+ // x.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
154
+ // {
155
+ // RoleClaimType = "permissions"
156
+ // };
157
+ // x.Events = new JwtBearerEvents
158
+ // {
159
+ // OnTokenValidated = async ctx =>
160
+ // {
161
+ // var accessToken = ctx.SecurityToken as JwtSecurityToken;
162
+ // if (accessToken != null)
163
+ // {
164
+ // var client = new HttpClient();
165
+ // var request = new HttpRequestMessage
166
+ // {
167
+ // Method = HttpMethod.Post,
168
+ // RequestUri = new Uri("https://dev-t61xkx2b.eu.auth0.com/userinfo")
169
+ // };
170
+ // request.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken.RawData);
171
+ // request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
103
172
104
- services . AddAuthentication ( OAuth2IntrospectionDefaults . AuthenticationScheme )
105
- . AddOAuth2Introspection ( x =>
173
+ // var message = await client.SendAsync(request);
174
+ // if (message.IsSuccessStatusCode)
175
+ // {
176
+ // var response = await message.Content.ReadAsStringAsync();
177
+ // var userInfo = JsonConvert.DeserializeObject<JObject>(response);
178
+ // if (ctx.Principal.Identity is ClaimsIdentity identity)
179
+ // {
180
+ // foreach (var cl in userInfo.Properties())
181
+ // {
182
+ // if (identity.Claims.Where(y => y.Type == cl.Name).Count() == 0)
183
+ // {
184
+ // identity.AddClaim(new Claim(cl.Name, cl.Value.Value<string>()));
185
+ // }
186
+ // }
187
+ // string perm = ctx.Principal.FindFirstValue("permissions");
188
+ // var claimRole = identity.Claims.Where(y => y.Type == ClaimTypes.Role).FirstOrDefault() ?? new Claim(ClaimTypes.Role, string.Empty);
189
+ // identity.AddClaim(new Claim(ClaimTypes.Role, string.Join(',', claimRole.Value, perm)));
190
+ // }
191
+ // }
192
+ // }
193
+ // }
194
+ // };
195
+
196
+ // x.Validate();
197
+ //})
198
+ //.AddScheme<RawIdentityServerAuthenticationOptions, RawLocalAccessTokenValidationHandler>("Bearer", (o) =>
199
+ //{
200
+ // o.AdminApiKey = this.config.AdminApiKey;
201
+ // o.ApiKey = this.config.ApiKey;
202
+ //})
203
+ // .AddScheme<RawIdentityServerAuthenticationOptions, RawLocalAccessTokenValidationHandler>("ApiKey", (o) =>
204
+ // {
205
+ // o.AdminApiKey = this.config.AdminApiKey;
206
+ // o.ApiKey = this.config.ApiKey;
207
+ // });
208
+
209
+ services . AddAuthorization ( options =>
106
210
{
107
- x = options ;
211
+ var policyBuilder = new AuthorizationPolicyBuilder ( )
212
+ . RequireAuthenticatedUser ( )
213
+ . AddAuthenticationSchemes ( schemeList . ToArray ( ) )
214
+ . Build ( ) ;
215
+
216
+ options . DefaultPolicy = policyBuilder ;
217
+ options . AddPolicy ( "rawCms" , policyBuilder ) ;
108
218
} ) ;
109
- }
110
- else
111
- {
112
- services
113
- . AddAuthentication ( ( options ) =>
114
- {
115
- options . DefaultScheme = "Bearer" ;
116
- options . DefaultChallengeScheme = "Bearer" ;
117
- //options.AddScheme("ApiKey", (x) => { x.HandlerType = typeof(RawLocalAccessTokenValidationHandler); });
118
- } )
119
- . AddJwtBearer ( "Bearer" + IdentityServerAuthenticationDefaults . AuthenticationScheme , ( options ) =>
120
- {
121
- options . RequireHttpsMetadata = false ;
122
- options . SaveToken = true ;
123
- options . IncludeErrorDetails = true ;
124
- options . TokenValidationParameters = new Microsoft . IdentityModel . Tokens . TokenValidationParameters ( )
125
- {
126
- RoleClaimType = ClaimTypes . Role ,
127
- NameClaimType = ClaimTypes . NameIdentifier
128
- } ;
129
- //options.Audience = IdentityServer4.IdentityServerConstants.LocalIdentityProvider;
130
- //options.Authority = IdentityServer4.IdentityServerConstants.LocalIdentityProvider;
131
- options . Validate ( ) ;
132
- } )
133
- . AddScheme < RawIdentityServerAuthenticationOptions , RawLocalAccessTokenValidationHandler > ( "Bearer" , ( o ) =>
134
- {
135
- o . AdminApiKey = this . config . AdminApiKey ;
136
- o . ApiKey = this . config . ApiKey ;
137
- } )
138
- . AddScheme < RawIdentityServerAuthenticationOptions , RawLocalAccessTokenValidationHandler > ( "ApiKey" , ( o ) =>
139
- {
140
- o . AdminApiKey = this . config . AdminApiKey ;
141
- o . ApiKey = this . config . ApiKey ;
142
- } ) ;
143
- }
144
219
}
145
220
146
221
public override void Configure ( IApplicationBuilder app )
0 commit comments