Commit e0c331e4 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Convert auth service to TS

parent 2274dd7a
import { TinyEmitter } from 'tiny-emitter'; import { TinyEmitter } from 'tiny-emitter';
import { serviceConfig } from '../config/service-config'; import { serviceConfig } from '../config/service-config';
import type { APIRoutesService } from './api-routes';
import type { LocalStorageService } from './local-storage';
import type { SidebarSettings } from '../../types/config';
import type { ToastMessengerService } from './toast-messenger';
import { OAuthClient } from '../util/oauth-client'; import { OAuthClient } from '../util/oauth-client';
import type { TokenInfo } from '../util/oauth-client';
import { resolve } from '../util/url'; import { resolve } from '../util/url';
/** type RefreshOptions = {
* @typedef {import('../util/oauth-client').TokenInfo} TokenInfo /**
* * True if access tokens should be persisted for use in future sessions.
* @typedef RefreshOptions
* @prop {boolean} persist - True if access tokens should be persisted for
* use in future sessions.
*/ */
persist: boolean;
};
/** /**
* Authorization service. * Authorization service.
...@@ -28,33 +32,32 @@ import { resolve } from '../util/url'; ...@@ -28,33 +32,32 @@ import { resolve } from '../util/url';
* @inject * @inject
*/ */
export class AuthService extends TinyEmitter { export class AuthService extends TinyEmitter {
/** public login: () => void;
* @param {Window} $window public logout: () => void;
* @param {import('./api-routes').APIRoutesService} apiRoutes public getAccessToken: () => void;
* @param {import('./local-storage').LocalStorageService} localStorage
* @param {import('./toast-messenger').ToastMessengerService} toastMessenger constructor(
* @param {import('../../types/config').SidebarSettings} settings $window: Window,
*/ apiRoutes: APIRoutesService,
constructor($window, apiRoutes, localStorage, settings, toastMessenger) { localStorage: LocalStorageService,
settings: SidebarSettings,
toastMessenger: ToastMessengerService
) {
super(); super();
/** /**
* Authorization code from auth popup window. * Authorization code from auth popup window.
* @type {string|null}
*/ */
let authCode; let authCode: string | null;
/** /**
* Token info retrieved via `POST /api/token` endpoint. * Token info retrieved via `POST /api/token` endpoint.
* *
* Resolves to `null` if the user is not logged in. * Resolves to `null` if the user is not logged in.
*
* @type {Promise<TokenInfo|null>|null}
*/ */
let tokenInfoPromise; let tokenInfoPromise: Promise<TokenInfo | null> | null;
/** @type {OAuthClient} */ let client: OAuthClient;
let client;
/** /**
* Absolute URL of the `/api/token` endpoint. * Absolute URL of the `/api/token` endpoint.
...@@ -63,10 +66,8 @@ export class AuthService extends TinyEmitter { ...@@ -63,10 +66,8 @@ export class AuthService extends TinyEmitter {
/** /**
* Show an error message telling the user that the access token has expired. * Show an error message telling the user that the access token has expired.
*
* @param {string} message
*/ */
function showAccessTokenExpiredErrorMessage(message) { function showAccessTokenExpiredErrorMessage(message: string) {
toastMessenger.error(`Hypothesis login lost: ${message}`, { toastMessenger.error(`Hypothesis login lost: ${message}`, {
autoDismiss: false, autoDismiss: false,
}); });
...@@ -114,10 +115,8 @@ export class AuthService extends TinyEmitter { ...@@ -114,10 +115,8 @@ export class AuthService extends TinyEmitter {
/** /**
* Persist access & refresh tokens for future use. * Persist access & refresh tokens for future use.
*
* @param {TokenInfo} token
*/ */
function saveToken(token) { function saveToken(token: TokenInfo) {
localStorage.setObject(storageKey(), token); localStorage.setObject(storageKey(), token);
} }
...@@ -152,12 +151,11 @@ export class AuthService extends TinyEmitter { ...@@ -152,12 +151,11 @@ export class AuthService extends TinyEmitter {
/** /**
* Exchange a refresh token for a new access token and refresh token pair. * Exchange a refresh token for a new access token and refresh token pair.
*
* @param {string} refreshToken
* @param {RefreshOptions} options
* @return {Promise<TokenInfo|null>} Promise for the new access token
*/ */
const refreshAccessToken = async (refreshToken, options) => { const refreshAccessToken = async (
refreshToken: string,
options: RefreshOptions
): Promise<TokenInfo | null> => {
const client = await oauthClient(); const client = await oauthClient();
let token; let token;
...@@ -186,9 +184,8 @@ export class AuthService extends TinyEmitter { ...@@ -186,9 +184,8 @@ export class AuthService extends TinyEmitter {
* Exchange authorization code retrieved from login popup for a new * Exchange authorization code retrieved from login popup for a new
* access token. * access token.
* *
* @param {string} code
*/ */
const exchangeAuthCodeForToken = async code => { const exchangeAuthCodeForToken = async (code: string) => {
const client = await oauthClient(); const client = await oauthClient();
const tokenInfo = await client.exchangeAuthCode(code); const tokenInfo = await client.exchangeAuthCode(code);
saveToken(tokenInfo); saveToken(tokenInfo);
...@@ -207,9 +204,9 @@ export class AuthService extends TinyEmitter { ...@@ -207,9 +204,9 @@ export class AuthService extends TinyEmitter {
/** /**
* Retrieve an access token for the API. * Retrieve an access token for the API.
* *
* @return {Promise<string|null>} The API access token or `null` if not logged in. * @return The API access token string or `null` if not logged in.
*/ */
const getAccessToken = async () => { const getAccessToken = async (): Promise<string | null> => {
// Determine how to get an access token, depending on the login method being used. // Determine how to get an access token, depending on the login method being used.
if (!tokenInfoPromise) { if (!tokenInfoPromise) {
const grantToken = getGrantToken(); const grantToken = getGrantToken();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment