Browse Source

refactor typing for authentication

modified-method-api
ojizero 4 years ago
parent
commit
f37a51ea78
No known key found for this signature in database
GPG Key ID: FEBB7097845B0C7E
  1. 35
      src/client.ts

35
src/client.ts

@ -4,21 +4,27 @@ import { OutgoingHttpHeaders } from 'http' @@ -4,21 +4,27 @@ import { OutgoingHttpHeaders } from 'http'
import defaultsDeep from 'lodash.defaultsdeep'
import { RequestOptions as HttpsRequestOptions } from 'https';
export enum AuthenticationTypes {
None = 'none',
BasicAuth = 'basic',
// TokenAuth = 'token',
// ApiKeyAuth = 'key',
BearerAuth = 'bearer',
interface BaseAuth {
type: 'basic' | 'bearer' | 'none',
}
export interface Authentication {
type: AuthenticationTypes,
username?: string,
password?: string,
authToken?: string,
export interface BasicAuth extends BaseAuth {
type: 'basic',
username: string,
password: string,
}
export interface BearerAuth extends BaseAuth {
type: 'bearer',
authToken: string,
}
export interface NoAuth extends BaseAuth {
type: 'none',
}
export type Authentication = NoAuth | BasicAuth | BearerAuth
interface AuthSpec {
useHeader: boolean,
usePayload: boolean,
@ -164,7 +170,7 @@ export class PortalClient implements Client { @@ -164,7 +170,7 @@ export class PortalClient implements Client {
setupAuthentication (auth: Authentication): AuthSpec {
switch (auth.type) {
case AuthenticationTypes.None: {
case 'none': {
return {
useHeader: false,
usePayload: false,
@ -173,7 +179,8 @@ export class PortalClient implements Client { @@ -173,7 +179,8 @@ export class PortalClient implements Client {
value: '',
}
}
case AuthenticationTypes.BasicAuth: {
case 'basic': {
const token = Buffer.from(`${auth.username}:${auth.password}`).toString('base64')
return {
@ -185,7 +192,7 @@ export class PortalClient implements Client { @@ -185,7 +192,7 @@ export class PortalClient implements Client {
}
}
case AuthenticationTypes.BearerAuth: {
case 'bearer': {
return {
useHeader: true,
usePayload: false,

Loading…
Cancel
Save