mirror of https://github.com/actions/toolkit
eslint fix
parent
af75719a1e
commit
0bab3623f4
|
@ -5,7 +5,6 @@ import * as core from '../src/core'
|
||||||
import {HttpClient} from '@actions/http-client'
|
import {HttpClient} from '@actions/http-client'
|
||||||
import {toCommandProperties} from '../src/utils'
|
import {toCommandProperties} from '../src/utils'
|
||||||
|
|
||||||
|
|
||||||
/* eslint-disable @typescript-eslint/unbound-method */
|
/* eslint-disable @typescript-eslint/unbound-method */
|
||||||
|
|
||||||
const testEnvVars = {
|
const testEnvVars = {
|
||||||
|
@ -437,12 +436,11 @@ function verifyFileCommand(command: string, expectedContents: string): void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTokenEndPoint() {
|
function getTokenEndPoint(): string {
|
||||||
return 'https://vstoken.actions.githubusercontent.com/.well-known/openid-configuration'
|
return 'https://vstoken.actions.githubusercontent.com/.well-known/openid-configuration'
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('oidc-client-tests', () => {
|
describe('oidc-client-tests', () => {
|
||||||
|
|
||||||
it('Get Http Client', async () => {
|
it('Get Http Client', async () => {
|
||||||
const http = new HttpClient('actions/oidc-client')
|
const http = new HttpClient('actions/oidc-client')
|
||||||
expect(http).toBeDefined()
|
expect(http).toBeDefined()
|
||||||
|
@ -453,5 +451,4 @@ describe('oidc-client-tests', () => {
|
||||||
const res = await http.get(getTokenEndPoint())
|
const res = await http.get(getTokenEndPoint())
|
||||||
expect(res.message.statusCode).toBe(200)
|
expect(res.message.statusCode).toBe(200)
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
|
@ -1,3 +1,4 @@
|
||||||
|
/* eslint-disable @typescript-eslint/no-extraneous-class */
|
||||||
import * as actions_http_client from '@actions/http-client'
|
import * as actions_http_client from '@actions/http-client'
|
||||||
import {IRequestOptions} from '@actions/http-client/interfaces'
|
import {IRequestOptions} from '@actions/http-client/interfaces'
|
||||||
import {HttpClient} from '@actions/http-client'
|
import {HttpClient} from '@actions/http-client'
|
||||||
|
@ -13,23 +14,27 @@ interface TokenResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class OidcClient {
|
export class OidcClient {
|
||||||
|
private static createHttpClient(
|
||||||
private static createHttpClient(allowRetry = true, maxRetry = 10) {
|
allowRetry = true,
|
||||||
let requestOptions: IRequestOptions = {
|
maxRetry = 10
|
||||||
|
): actions_http_client.HttpClient {
|
||||||
|
const requestOptions: IRequestOptions = {
|
||||||
allowRetries: allowRetry,
|
allowRetries: allowRetry,
|
||||||
maxRetries: maxRetry
|
maxRetries: maxRetry
|
||||||
}
|
}
|
||||||
|
|
||||||
return new HttpClient('actions/oidc-client', [
|
return new HttpClient(
|
||||||
new BearerCredentialHandler(OidcClient.getRuntimeToken())],
|
'actions/oidc-client',
|
||||||
requestOptions)
|
[new BearerCredentialHandler(OidcClient.getRuntimeToken())],
|
||||||
|
requestOptions
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private static getApiVersion(): string {
|
private static getApiVersion(): string {
|
||||||
return '2.0'
|
return '2.0'
|
||||||
}
|
}
|
||||||
|
|
||||||
private static getRuntimeToken(){
|
private static getRuntimeToken(): string {
|
||||||
const token = process.env['ACTIONS_RUNTIME_TOKEN']
|
const token = process.env['ACTIONS_RUNTIME_TOKEN']
|
||||||
if (!token) {
|
if (!token) {
|
||||||
throw new Error('Unable to get ACTIONS_RUNTIME_TOKEN env variable')
|
throw new Error('Unable to get ACTIONS_RUNTIME_TOKEN env variable')
|
||||||
|
@ -37,18 +42,23 @@ export class OidcClient {
|
||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
|
|
||||||
private static getIDTokenUrl(){
|
private static getIDTokenUrl(): string {
|
||||||
let runtimeUrl = process.env['ACTIONS_ID_TOKEN_REQUEST_URL']
|
const runtimeUrl = process.env['ACTIONS_ID_TOKEN_REQUEST_URL']
|
||||||
if (!runtimeUrl) {
|
if (!runtimeUrl) {
|
||||||
throw new Error('Unable to get ACTIONS_ID_TOKEN_REQUEST_URL env variable')
|
throw new Error('Unable to get ACTIONS_ID_TOKEN_REQUEST_URL env variable')
|
||||||
}
|
}
|
||||||
return runtimeUrl + '?api-version=' + OidcClient.getApiVersion()
|
return `${runtimeUrl}?api-version=${OidcClient.getApiVersion()}`
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async postCall(id_token_url: string, data: TokenRequest): Promise<string> {
|
private static async postCall(
|
||||||
|
id_token_url: string,
|
||||||
|
data: TokenRequest
|
||||||
|
): Promise<string> {
|
||||||
const httpclient = OidcClient.createHttpClient()
|
const httpclient = OidcClient.createHttpClient()
|
||||||
|
|
||||||
const res = await httpclient.postJson<TokenResponse>(id_token_url,data).catch((error) => {
|
const res = await httpclient
|
||||||
|
.postJson<TokenResponse>(id_token_url, data)
|
||||||
|
.catch(error => {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Failed to get ID Token. \n
|
`Failed to get ID Token. \n
|
||||||
Error Code : ${error.statusCode}\n
|
Error Code : ${error.statusCode}\n
|
||||||
|
@ -61,19 +71,18 @@ export class OidcClient {
|
||||||
throw new Error('Response json body do not have ID Token field')
|
throw new Error('Response json body do not have ID Token field')
|
||||||
}
|
}
|
||||||
return id_token
|
return id_token
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getIDToken(audience: string | undefined): Promise<string> {
|
static async getIDToken(audience?: string): Promise<string> {
|
||||||
try {
|
try {
|
||||||
// New ID Token is requested from action service
|
// New ID Token is requested from action service
|
||||||
const id_token_url: string = OidcClient.getIDTokenUrl()
|
const id_token_url: string = OidcClient.getIDTokenUrl()
|
||||||
|
|
||||||
debug(`ID token url is ${id_token_url}`)
|
debug(`ID token url is ${id_token_url}`)
|
||||||
|
|
||||||
const data: TokenRequest = { aud: audience }
|
const data: TokenRequest = {aud: audience}
|
||||||
|
|
||||||
debug(`audience is ${!!audience ? audience : 'not defined'}`)
|
debug(`audience is ${audience ? audience : 'not defined'}`)
|
||||||
|
|
||||||
const id_token = await OidcClient.postCall(id_token_url, data)
|
const id_token = await OidcClient.postCall(id_token_url, data)
|
||||||
setSecret(id_token)
|
setSecret(id_token)
|
||||||
|
|
Loading…
Reference in New Issue