Files
toolkit/packages/core/src/oidc-utils.ts
T

94 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-08-04 09:24:51 +05:30
import * as actions_http_client from '@actions/http-client'
2021-08-12 16:14:22 +05:30
import {IRequestOptions} from '@actions/http-client/interfaces'
2021-08-04 09:24:51 +05:30
import {HttpClient} from '@actions/http-client'
import {BearerCredentialHandler} from '@actions/http-client/auth'
2021-08-11 03:50:43 +05:30
import {debug, setSecret} from './core'
2021-08-04 09:24:51 +05:30
2021-08-09 06:36:02 +05:30
interface IOidcClient {
2021-08-04 09:24:51 +05:30
2021-08-09 06:36:02 +05:30
createHttpClient(): actions_http_client.HttpClient
getApiVersion(): string
getRuntimeToken(): string
getIDTokenUrl(): string
2021-08-12 10:07:18 +05:30
postCall(httpclient: actions_http_client.HttpClient, id_token_url: string, audience: string): Promise<string>
2021-08-09 06:36:02 +05:30
getIDToken(audience: string): Promise<string>
2021-08-04 09:24:51 +05:30
}
2021-08-09 06:36:02 +05:30
export class OidcClient implements IOidcClient {
createHttpClient(allowRetry = true, maxRetry = 10) {
let requestOptions : IRequestOptions = {}
requestOptions.allowRetries = allowRetry
requestOptions.maxRetries = maxRetry
return new HttpClient('actions/oidc-client', [
new BearerCredentialHandler(this.getRuntimeToken())],
requestOptions)
2021-08-04 09:24:51 +05:30
}
2021-08-09 06:36:02 +05:30
getApiVersion(): string {
return '2.0'
2021-08-04 09:24:51 +05:30
}
2021-08-09 06:36:02 +05:30
getRuntimeToken(){
const token = process.env['ACTIONS_RUNTIME_TOKEN']
if (!token) {
throw new Error('Unable to get ACTIONS_RUNTIME_TOKEN env variable')
}
return token
2021-08-04 09:24:51 +05:30
}
2021-08-09 06:36:02 +05:30
getIDTokenUrl(){
let runtimeUrl = process.env['ACTIONS_ID_TOKEN_REQUEST_URL']
if (!runtimeUrl) {
throw new Error('Unable to get ACTIONS_ID_TOKEN_REQUEST_URL env variable')
}
return runtimeUrl + '?api-version=' + this.getApiVersion()
}
2021-08-04 09:24:51 +05:30
2021-08-12 10:07:18 +05:30
async postCall(httpclient: actions_http_client.HttpClient, id_token_url: string, audience: string): Promise<string> {
const data = audience !== null ? {aud: audience} : ''
2021-08-04 09:24:51 +05:30
2021-08-09 06:36:02 +05:30
debug(`audience is ${audience !== null ? audience : 'null'}`)
2021-08-04 09:24:51 +05:30
2021-08-12 10:07:18 +05:30
const res = await httpclient.postJson(id_token_url,data).catch((error) => {
2021-08-09 06:36:02 +05:30
throw new Error(
2021-08-11 03:50:43 +05:30
`Failed to get ID Token. \n
2021-08-12 10:07:18 +05:30
Error Code : ${error.statusCode}\n
2021-08-12 16:14:22 +05:30
Error Message: ${error.result.message}`
2021-08-09 06:36:02 +05:30
)
2021-08-12 10:07:18 +05:30
})
2021-08-04 09:24:51 +05:30
2021-08-12 10:07:18 +05:30
let val :any = res.result
let id_token = val['value']
if (id_token === undefined) {
2021-08-09 06:36:02 +05:30
throw new Error('Response json body do not have ID Token field')
}
return id_token
2021-08-12 10:07:18 +05:30
2021-08-09 06:36:02 +05:30
}
async getIDToken(audience: string): Promise<string> {
try {
2021-08-12 10:07:18 +05:30
const httpclient = this.createHttpClient()
if (httpclient === undefined) {
throw new Error(`Failed to get Httpclient `)
}
2021-08-09 06:36:02 +05:30
// New ID Token is requested from action service
let id_token_url: string = this.getIDTokenUrl()
debug(`ID token url is ${id_token_url}`)
2021-08-04 09:24:51 +05:30
2021-08-12 10:07:18 +05:30
let id_token = await this.postCall(httpclient ,id_token_url, audience)
setSecret(id_token)
2021-08-09 06:36:02 +05:30
return id_token
} catch (error) {
throw new Error(`Error message: ${error.message}`)
}
2021-08-04 09:24:51 +05:30
}
}