From 8ea1c74153c85e5b7045ecdd173ddd72ac9116d6 Mon Sep 17 00:00:00 2001 From: Marcus Eide Date: Tue, 9 Jun 2020 12:01:29 +0200 Subject: [PATCH] Refactor auth provider classes to extend ObservableSession class --- .../auth/ObservableSession.tsx | 36 +++++++++++++++++++ .../implementations/auth/github/GithubAuth.ts | 25 +++---------- .../implementations/auth/google/GoogleAuth.ts | 26 +++----------- 3 files changed, 45 insertions(+), 42 deletions(-) create mode 100644 packages/core-api/src/apis/implementations/auth/ObservableSession.tsx diff --git a/packages/core-api/src/apis/implementations/auth/ObservableSession.tsx b/packages/core-api/src/apis/implementations/auth/ObservableSession.tsx new file mode 100644 index 0000000000..b080f64f08 --- /dev/null +++ b/packages/core-api/src/apis/implementations/auth/ObservableSession.tsx @@ -0,0 +1,36 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BehaviorSubject } from '../../../lib'; +import { Observable } from '../../../types'; + +export class ObservableSession { + private session: T | undefined; + private readonly subject = new BehaviorSubject(undefined); + + session$(): Observable { + return this.subject; + } + + getSession(): T | undefined { + return this.session; + } + + setSession(session?: T): void { + this.session = session; + this.subject.next(session); + } +} diff --git a/packages/core-api/src/apis/implementations/auth/github/GithubAuth.ts b/packages/core-api/src/apis/implementations/auth/github/GithubAuth.ts index d369e125af..bc93af8fc7 100644 --- a/packages/core-api/src/apis/implementations/auth/github/GithubAuth.ts +++ b/packages/core-api/src/apis/implementations/auth/github/GithubAuth.ts @@ -21,8 +21,7 @@ import { OAuthApi, AccessTokenOptions } from '../../../definitions/auth'; import { OAuthRequestApi, AuthProvider } from '../../../definitions'; import { SessionManager } from '../../../../lib/AuthSessionManager/types'; import { StaticAuthSessionManager } from '../../../../lib/AuthSessionManager'; -import { BehaviorSubject } from '../../../../lib'; -import { Observable } from '../../../../types'; +import { ObservableSession } from '../ObservableSession'; type CreateOptions = { // TODO(Rugvip): These two should be grabbed from global config when available, they're not unique to GithubAuth @@ -48,7 +47,7 @@ const DEFAULT_PROVIDER = { icon: GithubIcon, }; -class GithubAuth implements OAuthApi { +class GithubAuth extends ObservableSession implements OAuthApi { static create({ apiOrigin, basePath, @@ -80,24 +79,8 @@ class GithubAuth implements OAuthApi { return new GithubAuth(sessionManager); } - constructor(private readonly sessionManager: SessionManager) {} - - private session: GithubSession | undefined; - private readonly subject = new BehaviorSubject( - undefined, - ); - - session$(): Observable { - return this.subject; - } - - getSession(): GithubSession | undefined { - return this.session; - } - - setSession(session?: GithubSession): void { - this.session = session; - this.subject.next(session); + constructor(private readonly sessionManager: SessionManager) { + super(); } async getAccessToken(scope?: string, options?: AccessTokenOptions) { diff --git a/packages/core-api/src/apis/implementations/auth/google/GoogleAuth.ts b/packages/core-api/src/apis/implementations/auth/google/GoogleAuth.ts index d310cdff57..8de65ce01b 100644 --- a/packages/core-api/src/apis/implementations/auth/google/GoogleAuth.ts +++ b/packages/core-api/src/apis/implementations/auth/google/GoogleAuth.ts @@ -29,8 +29,7 @@ import { import { OAuthRequestApi, AuthProvider } from '../../../definitions'; import { SessionManager } from '../../../../lib/AuthSessionManager/types'; import { RefreshingAuthSessionManager } from '../../../../lib/AuthSessionManager'; -import { BehaviorSubject } from '../../../../lib'; -import { Observable } from '../../../../types'; +import { ObservableSession } from '../ObservableSession'; type CreateOptions = { // TODO(Rugvip): These two should be grabbed from global config when available, they're not unique to GoogleAuth @@ -59,7 +58,8 @@ const DEFAULT_PROVIDER = { const SCOPE_PREFIX = 'https://www.googleapis.com/auth/'; -class GoogleAuth implements OAuthApi, OpenIdConnectApi, ProfileInfoApi { +class GoogleAuth extends ObservableSession + implements OAuthApi, OpenIdConnectApi, ProfileInfoApi { static create({ apiOrigin, basePath, @@ -101,24 +101,8 @@ class GoogleAuth implements OAuthApi, OpenIdConnectApi, ProfileInfoApi { return new GoogleAuth(sessionManager); } - constructor(private readonly sessionManager: SessionManager) {} - - private session: GoogleSession | undefined; - private readonly subject = new BehaviorSubject( - undefined, - ); - - session$(): Observable { - return this.subject; - } - - getSession(): GoogleSession | undefined { - return this.session; - } - - setSession(session?: GoogleSession): void { - this.session = session; - this.subject.next(session); + constructor(private readonly sessionManager: SessionManager) { + super(); } async getAccessToken(