Refactor auth provider classes to extend ObservableSession class

This commit is contained in:
Marcus Eide
2020-06-09 12:01:29 +02:00
committed by Raghunandan
parent fa320b5e34
commit 8ea1c74153
3 changed files with 45 additions and 42 deletions
@@ -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<T> {
private session: T | undefined;
private readonly subject = new BehaviorSubject<T | undefined>(undefined);
session$(): Observable<T | undefined> {
return this.subject;
}
getSession(): T | undefined {
return this.session;
}
setSession(session?: T): void {
this.session = session;
this.subject.next(session);
}
}
@@ -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<GithubSession> implements OAuthApi {
static create({
apiOrigin,
basePath,
@@ -80,24 +79,8 @@ class GithubAuth implements OAuthApi {
return new GithubAuth(sessionManager);
}
constructor(private readonly sessionManager: SessionManager<GithubSession>) {}
private session: GithubSession | undefined;
private readonly subject = new BehaviorSubject<GithubSession | undefined>(
undefined,
);
session$(): Observable<GithubSession | undefined> {
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<GithubSession>) {
super();
}
async getAccessToken(scope?: string, options?: AccessTokenOptions) {
@@ -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<GoogleSession>
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<GoogleSession>) {}
private session: GoogleSession | undefined;
private readonly subject = new BehaviorSubject<GoogleSession | undefined>(
undefined,
);
session$(): Observable<GoogleSession | undefined> {
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<GoogleSession>) {
super();
}
async getAccessToken(