diff --git a/npm/ng-packs/packages/core/src/lib/abstracts/auth-response.model.ts b/npm/ng-packs/packages/core/src/lib/abstracts/auth-response.model.ts new file mode 100644 index 0000000000..7c8afaa3ec --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/abstracts/auth-response.model.ts @@ -0,0 +1,10 @@ +export interface AbpAuthResponse { + access_token: string; + id_token: string; + token_type: string; + expires_in: number; + refresh_token: string; + scope: string; + state?: string; + tenant_domain?: string; +} diff --git a/npm/ng-packs/packages/core/src/lib/abstracts/auth.service.ts b/npm/ng-packs/packages/core/src/lib/abstracts/auth.service.ts index 97bf0b7b24..3c6d62fae6 100644 --- a/npm/ng-packs/packages/core/src/lib/abstracts/auth.service.ts +++ b/npm/ng-packs/packages/core/src/lib/abstracts/auth.service.ts @@ -1,7 +1,9 @@ +import { HttpHeaders } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Params } from '@angular/router'; import { Observable, of } from 'rxjs'; import { LoginParams } from '../models/auth'; +import { AbpAuthResponse } from './auth-response.model'; /** * Abstract service for Authentication. @@ -10,8 +12,6 @@ import { LoginParams } from '../models/auth'; providedIn: 'root', }) export class AuthService implements IAuthService { - constructor() {} - private warningMessage() { console.error('You should add @abp/ng-oauth packages or create your own auth packages.'); } @@ -42,6 +42,15 @@ export class AuthService implements IAuthService { this.warningMessage(); return false; } + + loginUsingGrant( + grantType: string, + parameters: object, + headers?: HttpHeaders, + ): Promise { + console.log({ grantType, parameters, headers }); + return Promise.reject(new Error('not implemented')); + } } export interface IAuthService { @@ -56,4 +65,10 @@ export interface IAuthService { navigateToLogin(queryParams?: Params): void; login(params: LoginParams): Observable; + + loginUsingGrant( + grantType: string, + parameters: object, + headers?: HttpHeaders, + ): Promise; } diff --git a/npm/ng-packs/packages/core/src/lib/abstracts/index.ts b/npm/ng-packs/packages/core/src/lib/abstracts/index.ts index 6a32fcf8eb..e1ff2f106b 100644 --- a/npm/ng-packs/packages/core/src/lib/abstracts/index.ts +++ b/npm/ng-packs/packages/core/src/lib/abstracts/index.ts @@ -1,3 +1,4 @@ export * from './ng-model.component'; export * from './auth.guard'; export * from './auth.service'; +export * from './auth-response.model'; diff --git a/npm/ng-packs/packages/oauth/src/lib/services/oauth.service.ts b/npm/ng-packs/packages/oauth/src/lib/services/oauth.service.ts index a8f937e8ca..e929ab7c79 100644 --- a/npm/ng-packs/packages/oauth/src/lib/services/oauth.service.ts +++ b/npm/ng-packs/packages/oauth/src/lib/services/oauth.service.ts @@ -2,23 +2,27 @@ import { Injectable, Injector } from '@angular/core'; import { Params } from '@angular/router'; import { from, Observable, lastValueFrom } from 'rxjs'; import { filter, map, switchMap, take, tap } from 'rxjs/operators'; -import { IAuthService, LoginParams } from '@abp/ng.core'; +import { AbpAuthResponse, IAuthService, LoginParams } from '@abp/ng.core'; import { AuthFlowStrategy } from '../strategies'; import { EnvironmentService } from '@abp/ng.core'; import { AUTH_FLOW_STRATEGY } from '../tokens/auth-flow-strategy'; -import { AuthConfig, OAuthService } from "angular-oauth2-oidc"; +import { OAuthService } from 'angular-oauth2-oidc'; +import { HttpHeaders } from '@angular/common/http'; @Injectable({ providedIn: 'root', }) export class AbpOAuthService implements IAuthService { private strategy!: AuthFlowStrategy; + private readonly oAuthService: OAuthService; get isInternalAuth() { return this.strategy.isInternalAuth; } - constructor(protected injector: Injector, private oAuthService: OAuthService) {} + constructor(protected injector: Injector) { + this.oAuthService = this.injector.get(OAuthService); + } async init() { const environmentService = this.injector.get(EnvironmentService); @@ -54,4 +58,25 @@ export class AbpOAuthService implements IAuthService { get isAuthenticated(): boolean { return this.oAuthService.hasValidAccessToken(); } + + loginUsingGrant( + grantType: string, + parameters: object, + headers?: HttpHeaders, + ): Promise { + const { clientId: client_id, dummyClientSecret: client_secret } = this.oAuthService; + const access_token = this.oAuthService.getAccessToken(); + const p = { + access_token, + grant_type: grantType, + client_id, + ...parameters, + }; + + if (client_secret) { + p['client_secret'] = client_secret; + } + + return this.oAuthService.fetchTokenUsingGrant(grantType, p, headers); + } }