diff --git a/webapp/src/app/shared/services/credential.service.ts b/webapp/src/app/shared/services/credential.service.ts index e934860b..0163c1d1 100644 --- a/webapp/src/app/shared/services/credential.service.ts +++ b/webapp/src/app/shared/services/credential.service.ts @@ -10,55 +10,14 @@ import { } from 'rxjs'; import { AccessResponse, + AccessToken, AuthService, Configuration, + IdToken, LoginResponse, } from '../../api'; import { HttpClient, HttpHeaders } from '@angular/common/http'; -export interface AuthAccount { - i: string; - l: string; - o: string; - e: string; - on: string; -} - -export interface AuthScope { - t: string; - a: AuthAccount; - d: number | null; - r: string[]; -} - -export interface IdentityGrant { - token_type: string; - access_token: string; - expires_in: number; - scope: AuthScope; -} - -export interface AccessGrant { - token_type: string; - access_token: string; - expires_in: number; - scope: AuthScope; - refresh_token: string; -} - -interface TimedIdentityGrant { - grant: IdentityGrant; - expiration: number; - accountId: string; -} - -interface TimedAccessGrant { - grant: AccessGrant; - expiration: number; - realAccountId: string; - effectiveAccountId: string; -} - type LogoutResult = 'logged-out' | 'stopped-impersonating'; type UnauthorizedRecoveryResult = | 'logged-out' @@ -90,29 +49,29 @@ export class CredentialService { public readonly realAccountId = new BehaviorSubject(''); public readonly effectiveAccountId = new BehaviorSubject(''); - private readonly identityToken = new BehaviorSubject< - TimedIdentityGrant | undefined - >(undefined); - private readonly accessToken = new BehaviorSubject< - TimedAccessGrant | undefined - >(undefined); + + private readonly identityToken = new BehaviorSubject( + undefined, + ); + private readonly accessToken = new BehaviorSubject( + undefined, + ); private request = 0; constructor() { - this.accessToken.subscribe((token) => { + this.accessToken.subscribe((token: AccessToken | undefined) => { if (token) { - const r = token.grant.scope.r; - this.superadmin.next(r.includes('SUPER_ADMIN')); - this.admin.next(r.includes('ADMIN')); - this.login.next(token.grant.scope.a?.l ?? ''); - this.email.next(token.grant.scope.a?.e ?? ''); - this.organizationName.next(token.grant.scope.a?.on ?? ''); + this.superadmin.next(token.effective_role === 'SUPER_ADMIN'); + this.admin.next(token.effective_role === 'ADMIN'); + // this.login.next(token.grant.scope.a?.l ?? ''); + // this.email.next(token.grant.scope.a?.e ?? ''); + // this.organizationName.next(token.grant.scope.a?.on ?? ''); this.impersonating.next( - token.realAccountId !== token.effectiveAccountId, + token.real_account_id !== token.effective_account_id, ); - this.realAccountId.next(token.realAccountId); - this.effectiveAccountId.next(token.effectiveAccountId); + this.realAccountId.next(token.real_account_id); + this.effectiveAccountId.next(token.effective_account_id); } else { this.superadmin.next(false); this.admin.next(false); @@ -125,8 +84,8 @@ export class CredentialService { } }); const data = localStorage.getItem(CredentialService.KEY_GRANT); - const id = data ? (JSON.parse(data) as TimedIdentityGrant) : undefined; - if (id?.expiration && id.expiration >= Date.now()) { + const id = data ? (JSON.parse(data) as AccessToken) : undefined; + if (id && new Date(id.expires_at).getTime() >= Date.now()) { this.identityToken.next(id); this.validating.next(true); setTimeout(() => this.refreshAccessToken(id), 0); @@ -187,31 +146,33 @@ export class CredentialService { switchMap((response: LoginResponse) => { if (this.request != request) return of(undefined); - const token: TimedIdentityGrant = { - accountId, - grant: { - token_type: 'ID', - access_token: response.id_token.raw_token, - expires_in: Math.max( - new Date(response.id_token.expires_at).getTime() - - Math.floor(Date.now() / 1000), - 0, - ), - scope: this.buildIdentityScope(accountId, email), - }, - expiration: new Date(response.id_token.expires_at).getTime(), - }; - this.identityToken.next(token); + const idToken = response.id_token; + + // const token: TimedIdentityGrant = { + // accountId, + // grant: { + // token_type: 'ID', + // access_token: response.id_token.raw_token, + // expires_in: Math.max( + // new Date(response.id_token.expires_at).getTime() - + // Math.floor(Date.now() / 1000), + // 0, + // ), + // scope: this.buildIdentityScope(accountId, email), + // }, + // expiration: new Date(response.id_token.expires_at).getTime(), + // }; + this.identityToken.next(idToken); localStorage.setItem( CredentialService.KEY_GRANT, - JSON.stringify(token), + JSON.stringify(idToken), ); - return this.getAccessGrantForIdentity(token, request); + return this.getAccessGrantForIdentity(idToken, request); }), ); } - public refreshAccessToken(identityToken: TimedIdentityGrant) { + public refreshAccessToken(identityToken: IdToken) { const request = ++this.request; this.validating.next(true); this.accessToken.next(undefined); @@ -254,7 +215,6 @@ export class CredentialService { return this.getAccessGrantForIdentity( this.identityToken.value, request, - this.identityToken.value.accountId, ).pipe(tap({ error: () => this.validating.next(false) })); } @@ -355,9 +315,9 @@ export class CredentialService { // 1. If we have a valid access token, return it immediately and complete. if ( this.accessToken.value && - Date.now() < this.accessToken.value.expiration + Date.now() < new Date(this.accessToken.value.expires_at).getTime() ) { - return of(this.accessToken.value.grant.access_token); + return of(this.accessToken.value.raw_token); } // 2. If we don't have a token, and we ARE NOT currently validating, trigger a refresh. @@ -373,26 +333,23 @@ export class CredentialService { filter((isValidating) => !isValidating), // Wait until validating becomes false take(1), // Take exactly 1 emission and then automatically complete() map(() => - this.accessToken.value - ? this.accessToken.value.grant.access_token - : undefined, + this.accessToken.value ? this.accessToken.value.raw_token : undefined, ), ); } private getAccessGrantForIdentity( - identityToken: TimedIdentityGrant, + idToken: IdToken, request: number, effectiveAccountId?: string, ): Observable { - const accountId = - identityToken.accountId || String(identityToken.grant.scope.a?.i ?? ''); + const realAccountId = idToken.real_account_id; const headers = new HttpHeaders({ - Authorization: `Bearer ${identityToken.grant.access_token}`, + Authorization: `Bearer ${idToken.raw_token}`, }); const payload = { - real_account_id: accountId, - effective_account_id: effectiveAccountId ?? accountId, + real_account_id: realAccountId, + effective_account_id: effectiveAccountId ?? realAccountId, }; return this.httpClient @@ -401,30 +358,13 @@ export class CredentialService { withCredentials: true, }) .pipe( - tap((result) => { + tap((result: AccessResponse) => { if (this.request != request) return; - const scope = this.buildAccessScope( - payload, - identityToken.grant.scope.a?.e ?? '', - ); - const token: TimedAccessGrant = { - grant: { - token_type: 'ACCESS', - access_token: result.access_token.raw_token, - refresh_token: result.refresh_token.raw_token, - expires_in: Math.max( - new Date(result.access_token.expires_at).getTime() - - Math.floor(Date.now() / 1000), - 0, - ), - scope, - }, - expiration: new Date(result.access_token.expires_at).getTime(), - realAccountId: payload.real_account_id ?? accountId, - effectiveAccountId: payload.effective_account_id ?? accountId, - }; - this.accessToken.next(token); - this.organization.next(scope.a.o); + + const accessToken = result.access_token; + + this.accessToken.next(accessToken); + this.organization.next(accessToken.effective_organization_id); this.authenticated.next(true); this.validating.next(false); }), @@ -435,39 +375,4 @@ export class CredentialService { private get apiBasePath(): string { return this.apiConfiguration?.basePath ?? ''; } - - private buildIdentityScope(accountId: string, email: string): AuthScope { - return { - t: 'ID', - d: null, - r: [], - a: { - i: accountId, - l: email, - o: '', - e: email, - on: '', - }, - }; - } - - private buildAccessScope(payload: any, email: string): AuthScope { - const accountId = - payload.effective_account_id ?? payload.real_account_id ?? ''; - const organizationId = payload.organization_id ?? ''; - const role = String(payload.effective_role ?? ''); - - return { - t: 'ACCESS', - d: null, - r: role ? [role] : [], - a: { - i: accountId, - l: email || accountId, - o: organizationId, - e: email, - on: organizationId, - }, - }; - } }