1- import { SERVICE_ACCOUNT_LOGIN } from "../common/api" ;
1+ import { SERVICE_ACCOUNT_LOGIN , USERNAME_PASSWORD_LOGIN } from "../common/api" ;
22import { assignProtocol } from "../common/util" ;
33import {
44 Context ,
55 ConnectionOptions ,
6- ClientCredentialsAuth ,
7- AccessTokenAuth
6+ ServiceAccountAuth ,
7+ UsernamePasswordAuth
88} from "../types" ;
99
1010type Login = {
@@ -21,7 +21,6 @@ export class Authenticator {
2121 options : ConnectionOptions ;
2222
2323 accessToken ?: string ;
24- refreshToken ?: string ;
2524
2625 constructor ( context : Context , options : ConnectionOptions ) {
2726 context . httpClient . authenticator = this ;
@@ -38,12 +37,7 @@ export class Authenticator {
3837 return { } ;
3938 }
4039
41- authenticateWithToken ( auth : AccessTokenAuth ) {
42- const { accessToken } = auth ;
43- this . accessToken = accessToken ;
44- }
45-
46- getAuthEndpoint ( apiEndpoint : string ) {
40+ private static getAuthEndpoint ( apiEndpoint : string ) {
4741 const myURL = new URL ( assignProtocol ( apiEndpoint ) ) ;
4842 const hostStrings = myURL . hostname . split ( "." ) ;
4943 // We expect an apiEndpoint to be of format api.<env>.firebolt.io
@@ -56,11 +50,32 @@ export class Authenticator {
5650 return myURL . toString ( ) ;
5751 }
5852
59- async authenticateServiceAccount ( auth : ClientCredentialsAuth ) {
53+ private async authenticateUsernamePassword ( auth : UsernamePasswordAuth ) {
54+ const { httpClient, apiEndpoint } = this . context ;
55+ const { username, password } = auth ;
56+ const url = `${ apiEndpoint } /${ USERNAME_PASSWORD_LOGIN } ` ;
57+ const body = JSON . stringify ( {
58+ username,
59+ password
60+ } ) ;
61+
62+ this . accessToken = undefined ;
63+
64+ const { access_token } = await httpClient
65+ . request < Login > ( "POST" , url , {
66+ body,
67+ retry : false
68+ } )
69+ . ready ( ) ;
70+
71+ this . accessToken = access_token ;
72+ }
73+
74+ private async authenticateServiceAccount ( auth : ServiceAccountAuth ) {
6075 const { httpClient, apiEndpoint } = this . context ;
6176 const { client_id, client_secret } = auth ;
6277
63- const authEndpoint = this . getAuthEndpoint ( apiEndpoint ) ;
78+ const authEndpoint = Authenticator . getAuthEndpoint ( apiEndpoint ) ;
6479 const params = new URLSearchParams ( {
6580 client_id,
6681 client_secret,
@@ -84,18 +99,30 @@ export class Authenticator {
8499 this . accessToken = access_token ;
85100 }
86101
87- async authenticate ( ) {
102+ isUsernamePassword ( ) {
88103 const options = this . options . auth || this . options ;
104+ return ! ! (
105+ ( options as UsernamePasswordAuth ) . username &&
106+ ( options as UsernamePasswordAuth ) . password
107+ ) ;
108+ }
89109
90- if ( ( options as AccessTokenAuth ) . accessToken ) {
91- this . authenticateWithToken ( options as AccessTokenAuth ) ;
110+ isServiceAccount ( ) {
111+ const options = this . options . auth || this . options ;
112+ return ! ! (
113+ ( options as ServiceAccountAuth ) . client_id &&
114+ ( options as ServiceAccountAuth ) . client_secret
115+ ) ;
116+ }
117+
118+ async authenticate ( ) {
119+ const options = this . options . auth || this . options ;
120+ if ( this . isUsernamePassword ( ) ) {
121+ await this . authenticateUsernamePassword ( options as UsernamePasswordAuth ) ;
92122 return ;
93123 }
94- if (
95- ( options as ClientCredentialsAuth ) . client_id &&
96- ( options as ClientCredentialsAuth ) . client_secret
97- ) {
98- await this . authenticateServiceAccount ( options as ClientCredentialsAuth ) ;
124+ if ( this . isServiceAccount ( ) ) {
125+ await this . authenticateServiceAccount ( options as ServiceAccountAuth ) ;
99126 return ;
100127 }
101128
0 commit comments