mirror of
https://git.mirrors.martin98.com/https://github.com/actions/toolkit
synced 2026-04-23 00:18:05 +08:00
oidc client changes
This commit is contained in:
@@ -1,54 +1,55 @@
|
||||
import * as core from '@actions/core'
|
||||
import {IHeaders} from '@actions/http-client/interfaces'
|
||||
import * as actions_http_client from '@actions/http-client'
|
||||
import {
|
||||
createHttpClient,
|
||||
isSuccessStatusCode
|
||||
isSuccessStatusCode,
|
||||
getApiVersion
|
||||
} from './internal/utils'
|
||||
|
||||
import {
|
||||
getIDTokenFromEnv,
|
||||
getIDTokenUrl
|
||||
} from './internal/config-variables'
|
||||
|
||||
import {getIDTokenFromEnv, getIDTokenUrl} from './internal/config-variables'
|
||||
|
||||
export async function getIDToken(audience: string): Promise<string> {
|
||||
try {
|
||||
|
||||
//Check if id token is stored in environment variable
|
||||
|
||||
var id_token: string = getIDTokenFromEnv()
|
||||
if(id_token != undefined) {
|
||||
let id_token: string = getIDTokenFromEnv()
|
||||
if (id_token !== undefined) {
|
||||
const secondsSinceEpoch = Math.round(Date.now() / 1000)
|
||||
const id_token_json = JSON.parse(id_token)
|
||||
if(parseInt(id_token_json['exp']) - secondsSinceEpoch > 120) // Expiry time is more than 2 mins
|
||||
if (parseInt(id_token_json['exp']) - secondsSinceEpoch > 120)
|
||||
// Expiry time is more than 2 mins
|
||||
return id_token
|
||||
}
|
||||
|
||||
|
||||
// New ID Token is requested from action service
|
||||
|
||||
const id_tokne_url: string = getIDTokenUrl()
|
||||
|
||||
if (id_tokne_url == undefined) {
|
||||
let id_token_url: string = getIDTokenUrl()
|
||||
|
||||
if (id_token_url === undefined) {
|
||||
throw new Error(`ID Token URL not found`)
|
||||
}
|
||||
|
||||
core.debug(`ID token url is ${id_tokne_url}`)
|
||||
id_token_url = id_token_url + '?api-version=' + getApiVersion()
|
||||
core.debug(`ID token url is ${id_token_url}`)
|
||||
|
||||
const httpclient = createHttpClient()
|
||||
if (httpclient == undefined) {
|
||||
if (httpclient === undefined) {
|
||||
throw new Error(`Failed to get Httpclient `)
|
||||
}
|
||||
core.debug(`Httpclient created ${httpclient} `) // debug is only output if you set the secret `ACTIONS_RUNNER_DEBUG` to true
|
||||
|
||||
var additionalHeaders = {[httpclient.Headers.ContentType]: httpclient.MediaTypes.ApplicationJson}
|
||||
const additionalHeaders = {
|
||||
[actions_http_client.Headers.ContentType]:
|
||||
actions_http_client.MediaTypes.ApplicationJson
|
||||
}
|
||||
|
||||
var data : String = new String('id_token_aud:')
|
||||
data = data.concat(audience)
|
||||
const response = await httpclient.post(id_tokne_url, data, additionalHeaders)
|
||||
const data: string = JSON.stringify({aud: audience})
|
||||
const response = await httpclient.post(
|
||||
id_token_url,
|
||||
data,
|
||||
additionalHeaders
|
||||
)
|
||||
|
||||
|
||||
if (!isSuccessStatusCode(response.message.statusCode)){
|
||||
if (!isSuccessStatusCode(response.message.statusCode)) {
|
||||
throw new Error(
|
||||
`Failed to get ID Token. Error message :${response.message.statusMessage} `
|
||||
)
|
||||
@@ -58,7 +59,7 @@ export async function getIDToken(audience: string): Promise<string> {
|
||||
const val = JSON.parse(body)
|
||||
id_token = val['value']
|
||||
|
||||
if (id_token == undefined) {
|
||||
if (id_token === undefined) {
|
||||
throw new Error(`Not able to fetch the ID token`)
|
||||
}
|
||||
|
||||
@@ -66,11 +67,12 @@ export async function getIDToken(audience: string): Promise<string> {
|
||||
core.exportVariable('OIDC_TOKEN_ID', id_token)
|
||||
|
||||
return id_token
|
||||
|
||||
} catch (error) {
|
||||
core.setFailed(error.message)
|
||||
return error.message
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.getIDToken = getIDToken
|
||||
//module.exports.getIDToken = getIDToken
|
||||
|
||||
getIDToken('helloworld')
|
||||
|
||||
Reference in New Issue
Block a user