Merge pull request #15788 from abpframework/issue/15713_dd_grant_function_on_AuthService

add grant function on AuthService
pull/15820/head
Muhammed Altuğ 3 years ago committed by GitHub
commit 1ae688c019
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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;
}

@ -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<AbpAuthResponse> {
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<any>;
loginUsingGrant(
grantType: string,
parameters: object,
headers?: HttpHeaders,
): Promise<AbpAuthResponse>;
}

@ -1,3 +1,4 @@
export * from './ng-model.component';
export * from './auth.guard';
export * from './auth.service';
export * from './auth-response.model';

@ -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<AbpAuthResponse> {
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);
}
}

Loading…
Cancel
Save