Skip to content

Commit db2d375

Browse files
committed
feat: full oidc working with PKCE
1 parent a327274 commit db2d375

File tree

5 files changed

+60
-19
lines changed

5 files changed

+60
-19
lines changed

Solution 6 - OIDC and Angular client/AuthorizationServer/Config.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static IEnumerable<IdentityResource> GetIdentityResources()
2727
};
2828
}
2929

30-
private static string spaClientUrl = "https://localhost:53709";
30+
private static string spaClientUrl = "https://localhost:44311";
3131

3232
public static IEnumerable<Client> GetClients()
3333
{
@@ -93,7 +93,7 @@ public static IEnumerable<Client> GetClients()
9393
{
9494
ClientId = "spaCodeClient",
9595
ClientName = "SPA Code Client",
96-
AccessTokenType = AccessTokenType.Reference,
96+
AccessTokenType = AccessTokenType.Jwt,
9797
// RequireConsent = false,
9898
AccessTokenLifetime = 330,// 330 seconds, default 60 minutes
9999
IdentityTokenLifetime = 30,
@@ -105,7 +105,7 @@ public static IEnumerable<Client> GetClients()
105105
AllowAccessTokensViaBrowser = true,
106106
RedirectUris = new List<string>
107107
{
108-
$"{spaClientUrl}",
108+
$"{spaClientUrl}/callback",
109109
$"{spaClientUrl}/silent-renew.html",
110110
"https://localhost:4200",
111111
"https://localhost:4200/silent-renew.html"

Solution 6 - OIDC and Angular client/ClientApp/ClientApp/src/app/core/auth/auth.service.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Injectable, OnInit, OnDestroy, Inject } from '@angular/core';
2-
import { OidcSecurityService, OpenIdConfiguration, AuthWellKnownEndpoints } from 'angular-auth-oidc-client';
2+
import { OidcSecurityService, OpenIdConfiguration, AuthWellKnownEndpoints, AuthorizationResult, AuthorizationState } from 'angular-auth-oidc-client';
33
import { Observable , Subscription } from 'rxjs';
44
import { HttpHeaders, HttpClient } from '@angular/common/http';
5+
import { Router } from '@angular/router';
56

67
@Injectable()
78
export class AuthService implements OnDestroy {
@@ -11,6 +12,7 @@ export class AuthService implements OnDestroy {
1112
constructor(
1213
private oidcSecurityService: OidcSecurityService,
1314
private http: HttpClient,
15+
private router: Router,
1416
@Inject('BASE_URL') private originUrl: string,
1517
@Inject('AUTH_URL') private authUrl: string,
1618
) {
@@ -28,12 +30,16 @@ export class AuthService implements OnDestroy {
2830
const openIdImplicitFlowConfiguration: OpenIdConfiguration = {
2931
stsServer: this.authUrl,
3032
redirect_url: this.originUrl + 'callback',
31-
client_id: 'spaClient',
32-
response_type: 'id_token token',
33+
client_id: 'spaCodeClient',
34+
response_type: 'code',
3335
scope: 'openid profile resourceApi',
3436
post_logout_redirect_uri: this.originUrl,
3537
forbidden_route: '/forbidden',
3638
unauthorized_route: '/unauthorized',
39+
start_checksession: true,
40+
silent_renew: true,
41+
silent_renew_url: this.originUrl + '/silent-renew.html',
42+
history_cleanup_off: true,
3743
auto_userinfo: true,
3844
log_console_warning_active: true,
3945
log_console_debug_active: true,
@@ -64,22 +70,32 @@ export class AuthService implements OnDestroy {
6470
this.isAuthorizedSubscription = this.oidcSecurityService.getIsAuthorized().subscribe((isAuthorized => {
6571
this.isAuthorized = isAuthorized;
6672
}));
73+
74+
this.oidcSecurityService.onAuthorizationResult.subscribe(
75+
(authorizationResult: AuthorizationResult) => {
76+
this.onAuthorizationResultComplete(authorizationResult);
77+
});
6778
}
6879

80+
private onAuthorizationResultComplete(authorizationResult: AuthorizationResult) {
81+
82+
console.log('Auth result received AuthorizationState:'
83+
+ authorizationResult.authorizationState
84+
+ ' validationResult:' + authorizationResult.validationResult);
85+
86+
if (authorizationResult.authorizationState === AuthorizationState.unauthorized) {
87+
if (window.parent) {
88+
// sent from the child iframe, for example the silent renew
89+
this.router.navigate(['/unauthorized']);
90+
} else {
91+
window.location.href = '/unauthorized';
92+
}
93+
}
94+
}
6995

7096
private doCallbackLogicIfRequired() {
7197

7298
this.oidcSecurityService.authorizedCallbackWithCode(window.location.toString());
73-
// if (window.location.hash) {
74-
// window.location.hash = decodeURIComponent(window.location.hash);
75-
// // authorizedCallback returns wrong result when hash is URI encoded
76-
// } else {
77-
78-
// this.oidcSecurityService.authorize();
79-
// }
80-
// if (typeof location !== "undefined") {
81-
// this.oidcSecurityService.authorizedCallback();
82-
// }
8399
}
84100

85101
getIsAuthorized(): Observable<boolean> {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<base href="./">
5+
<meta charset="utf-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>silent-renew</title>
8+
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
9+
</head>
10+
<body>
11+
12+
<script>
13+
window.onload = function () {
14+
/* The parent window hosts the Angular application */
15+
var parent = window.parent;
16+
/* Send the id_token information to the oidc message handler */
17+
var event = new CustomEvent('oidc-silent-renew-message', { detail: window.location });
18+
parent.dispatchEvent(event);
19+
};
20+
</script>
21+
</body>
22+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin/
2+
obj/

Solution 6 - OIDC and Angular client/ResourceApi/Startup.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Builder;
1+
using IdentityServer4.AccessTokenValidation;
2+
using Microsoft.AspNetCore.Builder;
23
using Microsoft.AspNetCore.Hosting;
34
using Microsoft.Extensions.Configuration;
45
using Microsoft.Extensions.DependencyInjection;
@@ -21,11 +22,11 @@ public void ConfigureServices(IServiceCollection services)
2122
.AddAuthorization()
2223
.AddJsonFormatters();
2324

24-
services.AddAuthentication("Bearer")
25+
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
2526
.AddIdentityServerAuthentication(options =>
2627
{
2728
options.Authority = "https://localhost:44370";
28-
options.RequireHttpsMetadata = false;
29+
options.RequireHttpsMetadata = true;
2930
options.ApiName = "resourceApi";
3031
});
3132

0 commit comments

Comments
 (0)