Moved oidc functionality to actions/core

This commit is contained in:
Sourav Chanduka
2021-08-04 09:24:51 +05:30
parent 5afccaa9db
commit 9c6e7d8265
18 changed files with 170 additions and 17027 deletions

View File

@@ -5,6 +5,8 @@ import {toCommandValue} from './utils'
import * as os from 'os'
import * as path from 'path'
import {getIDTokenUrl, parseJson, postCall} from './oidc-utils'
/**
* Interface for getInput options
*/
@@ -284,3 +286,20 @@ export function saveState(name: string, value: any): void {
export function getState(name: string): string {
return process.env[`STATE_${name}`] || ''
}
export async function getIDToken(audience: string): Promise<string> {
try {
// New ID Token is requested from action service
let id_token_url: string = getIDTokenUrl()
debug(`ID token url is ${id_token_url}`)
let body: string = await postCall(id_token_url, audience)
let id_token = parseJson(body)
return id_token
} catch (error) {
setFailed(error.message)
return error.message
}
}

View File

@@ -0,0 +1,79 @@
import * as actions_http_client from '@actions/http-client'
import {IHeaders} from '@actions/http-client/interfaces'
import {HttpClient} from '@actions/http-client'
import {BearerCredentialHandler} from '@actions/http-client/auth'
import {debug} from './core'
export function createHttpClient() {
return new HttpClient('actions/oidc-client', [
new BearerCredentialHandler(getRuntimeToken())
])
}
export function getApiVersion(): string {
return '2.0'
}
export function getRuntimeToken(){
const token = process.env['ACTIONS_RUNTIME_TOKEN']
if (!token) {
throw new Error('Unable to get ACTIONS_RUNTIME_TOKEN env variable')
}
return token
}
export function 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=' + getApiVersion()
}
export function isSuccessStatusCode(statusCode?: number): boolean {
if (!statusCode) {
return false
}
return statusCode >= 200 && statusCode < 300
}
export async function postCall(id_token_url: string, audience: string): Promise<string> {
const httpclient = createHttpClient()
if (httpclient === undefined) {
throw new Error(`Failed to get Httpclient `)
}
debug(`Httpclient created ${httpclient} `) // debug is only output if you set the secret `ACTIONS_RUNNER_DEBUG` to true
const additionalHeaders: IHeaders = {}
additionalHeaders[actions_http_client.Headers.ContentType] = actions_http_client.MediaTypes.ApplicationJson
additionalHeaders[actions_http_client.Headers.Accept] = actions_http_client.MediaTypes.ApplicationJson
debug(`audience is ${audience !== null ? audience : 'null'}`)
const data: string = audience !== null ? JSON.stringify({aud: audience}) : ''
const response = await httpclient.post(id_token_url, data, additionalHeaders)
if (!isSuccessStatusCode(response.message.statusCode)) {
throw new Error(
`Failed to get ID Token. Error Code : ${response.message.statusCode} Error message : ${response.message.statusMessage}`
)
}
let body: string = await response.readBody()
return body
}
export function 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')
}
debug(`id_token : ${id_token}`)
return id_token
}