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

120 lines
3.4 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-09 06:36:02 +05:30
import {IHeaders,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
isSuccessStatusCode(statusCode?: number): boolean
postCall(id_token_url: string, audience: string): Promise<string>
2021-08-04 09:24:51 +05:30
2021-08-09 06:36:02 +05:30
parseJson(body: string): string
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-09 06:36:02 +05:30
isSuccessStatusCode(statusCode?: number): boolean {
if (!statusCode) {
return false
}
return statusCode >= 200 && statusCode < 300
2021-08-04 09:24:51 +05:30
}
2021-08-09 06:36:02 +05:30
async postCall(id_token_url: string, audience: string): Promise<string> {
const httpclient = this.createHttpClient()
if (httpclient === undefined) {
throw new Error(`Failed to get Httpclient `)
}
let additionalHeaders: IHeaders = {}
additionalHeaders[actions_http_client.Headers.ContentType] = actions_http_client.MediaTypes.ApplicationJson
additionalHeaders[actions_http_client.Headers.Accept] = actions_http_client.MediaTypes.ApplicationJson
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-09 06:36:02 +05:30
const data: string = audience !== null ? JSON.stringify({aud: audience}) : ''
const response = await httpclient.post(id_token_url, data, additionalHeaders)
2021-08-10 15:36:13 +05:30
const body: string = await response.readBody()
2021-08-04 09:24:51 +05:30
2021-08-09 06:36:02 +05:30
if (!this.isSuccessStatusCode(response.message.statusCode)) {
throw new Error(
2021-08-11 03:50:43 +05:30
`Failed to get ID Token. \n
Error Code : ${response.message.statusCode} Error message : ${response.message.statusMessage} \n
Response body: ${body}`
2021-08-09 06:36:02 +05:30
)
}
return body
2021-08-04 09:24:51 +05:30
}
2021-08-09 06:36:02 +05:30
parseJson(body: string): string {
const val = JSON.parse(body)
let id_token = ''
if ('value' in val) {
id_token = val['value']
} else {
throw new Error('Response json body do not have ID Token field')
}
2021-08-11 03:50:43 +05:30
setSecret(id_token)
2021-08-09 06:36:02 +05:30
return id_token
}
async getIDToken(audience: string): Promise<string> {
try {
// 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-09 06:36:02 +05:30
let body: string = await this.postCall(id_token_url, audience)
let id_token = this.parseJson(body)
return id_token
} catch (error) {
throw new Error(`Error message: ${error.message}`)
}
2021-08-04 09:24:51 +05:30
}
2021-08-09 06:36:02 +05:30
2021-08-04 09:24:51 +05:30
}