1
1
using System . Text . Json . Serialization ;
2
2
3
+ using Altinn . Broker . API . Configuration ;
4
+ using Altinn . Broker . API . Models ;
3
5
using Altinn . Broker . Application ;
4
- using Altinn . Broker . Helpers ;
5
6
using Altinn . Broker . Integrations ;
6
7
using Altinn . Broker . Integrations . Azure ;
7
8
using Altinn . Broker . Integrations . Hangfire ;
8
9
using Altinn . Broker . Middlewares ;
9
- using Altinn . Broker . Models . Maskinporten ;
10
10
using Altinn . Broker . Persistence ;
11
11
using Altinn . Broker . Persistence . Options ;
12
+ using Altinn . Common . PEP . Authorization ;
12
13
13
14
using Hangfire ;
14
15
15
16
using Microsoft . ApplicationInsights . Extensibility ;
16
17
using Microsoft . AspNetCore . Authentication . JwtBearer ;
18
+ using Microsoft . AspNetCore . Authorization ;
17
19
using Microsoft . AspNetCore . Http . Features ;
18
20
using Microsoft . AspNetCore . Server . Kestrel . Core ;
19
- using Microsoft . IdentityModel . JsonWebTokens ;
20
21
using Microsoft . IdentityModel . Tokens ;
21
22
22
23
using Serilog ;
@@ -97,7 +98,7 @@ static void ConfigureServices(IServiceCollection services, IConfiguration config
97
98
98
99
services . Configure < DatabaseOptions > ( config . GetSection ( key : nameof ( DatabaseOptions ) ) ) ;
99
100
services . Configure < AzureResourceManagerOptions > ( config . GetSection ( key : nameof ( AzureResourceManagerOptions ) ) ) ;
100
- services . Configure < MaskinportenOptions > ( config . GetSection ( key : nameof ( MaskinportenOptions ) ) ) ;
101
+ services . Configure < AltinnOptions > ( config . GetSection ( key : nameof ( AltinnOptions ) ) ) ;
101
102
102
103
services . AddHttpClient ( ) ;
103
104
services . AddProblemDetails ( ) ;
@@ -106,47 +107,29 @@ static void ConfigureServices(IServiceCollection services, IConfiguration config
106
107
107
108
services . AddAuthentication ( JwtBearerDefaults . AuthenticationScheme ) . AddJwtBearer ( options =>
108
109
{
109
- var maskinportenOptions = new MaskinportenOptions ( ) ;
110
- config . GetSection ( nameof ( MaskinportenOptions ) ) . Bind ( maskinportenOptions ) ;
110
+ var altinnOptions = new AltinnOptions ( ) ;
111
+ config . GetSection ( nameof ( AltinnOptions ) ) . Bind ( altinnOptions ) ;
111
112
options . SaveToken = true ;
112
- options . MetadataAddress = $ " { maskinportenOptions . Issuer } .well-known/oauth-authorization-server" ;
113
- if ( hostEnvironment . IsDevelopment ( ) )
113
+ options . MetadataAddress = altinnOptions . OpenIdWellKnown ;
114
+ options . TokenValidationParameters = new TokenValidationParameters
114
115
{
115
- options . TokenValidationParameters = new TokenValidationParameters
116
- {
117
- ValidateIssuer = false ,
118
- ValidateAudience = false ,
119
- ValidateLifetime = false ,
120
- RequireExpirationTime = false ,
121
- RequireSignedTokens = false ,
122
- SignatureValidator = delegate ( string token , TokenValidationParameters parameters )
123
- {
124
- var jwt = new JsonWebToken ( token ) ;
125
- return jwt ;
126
- }
127
- } ;
128
- }
129
- else
130
- {
131
- options . TokenValidationParameters = new TokenValidationParameters
132
- {
133
- ValidIssuer = maskinportenOptions . Issuer ,
134
- ValidateIssuer = true ,
135
- ValidateAudience = false ,
136
- ValidateLifetime = true ,
137
- RequireExpirationTime = true ,
138
- RequireSignedTokens = true
139
- } ;
140
- }
116
+ ValidateIssuerSigningKey = true ,
117
+ ValidateIssuer = false ,
118
+ ValidateAudience = false ,
119
+ RequireExpirationTime = true ,
120
+ ValidateLifetime = true ,
121
+ ClockSkew = TimeSpan . Zero
122
+ } ;
141
123
} ) ;
142
124
125
+ services . AddTransient < IAuthorizationHandler , ScopeAccessHandler > ( ) ;
143
126
services . AddAuthorization ( options =>
144
127
{
145
- options . AddPolicy ( "ResourceOwner" , policy => policy . RequireClaim ( "scope" , [ "altinn:broker.admin" ] ) ) ;
146
- options . AddPolicy ( "Sender" , policy => policy . RequireClaim ( "scope" , [ "altinn:broker.write" , "altinn:broker.write altinn:broker.read" ] ) ) ;
147
- options . AddPolicy ( " Recipient" , policy => policy . RequireClaim ( "scope" , [ "altinn:broker.read" , "altinn:broker.write altinn:broker.read" ] ) ) ;
148
- options . AddPolicy ( " SenderOrRecipient" , policy => policy . RequireClaim ( "scope" , [ "altinn:broker.read" , "altinn:broker.write" , "altinn:broker.write altinn:broker.read" ] ) ) ;
149
- options . AddPolicy ( " Legacy" , policy => policy . RequireClaim ( "scope" , [ "altinn:broker.legacy" ] ) ) ;
128
+ options . AddPolicy ( AuthorizationConstants . Sender , policy => policy . AddRequirements ( new ScopeAccessRequirement ( AuthorizationConstants . SenderScope ) ) ) ;
129
+ options . AddPolicy ( AuthorizationConstants . ResourceOwner , policy => policy . AddRequirements ( new ScopeAccessRequirement ( AuthorizationConstants . AdminScope ) ) ) ;
130
+ options . AddPolicy ( AuthorizationConstants . Recipient , policy => policy . AddRequirements ( new ScopeAccessRequirement ( AuthorizationConstants . RecipientScope ) ) ) ;
131
+ options . AddPolicy ( AuthorizationConstants . SenderOrRecipient , policy => policy . AddRequirements ( new ScopeAccessRequirement ( [ AuthorizationConstants . SenderScope , AuthorizationConstants . RecipientScope ] ) ) ) ;
132
+ options . AddPolicy ( AuthorizationConstants . Legacy , policy => policy . AddRequirements ( new ScopeAccessRequirement ( AuthorizationConstants . LegacyScope ) ) ) ;
150
133
} ) ;
151
134
152
135
services . Configure < KestrelServerOptions > ( options =>
0 commit comments