mirror of https://github.com/actions/toolkit
Resolved Comments
parent
ff90431d27
commit
0c1cb726c3
|
@ -1,6 +1,8 @@
|
|||
var httpclient = require('@actions/http-client')
|
||||
var configvar = require('./../src/internal/config-variables')
|
||||
var main = require('./../src/main')
|
||||
|
||||
function getTokenEndPoint() {
|
||||
return 'https://vstoken.actions.githubusercontent.com/.well-known/openid-configuration'
|
||||
}
|
||||
|
||||
describe('oidc-client-tests', () => {
|
||||
it('Get Http Client', async () => {
|
||||
|
@ -10,23 +12,7 @@ describe('oidc-client-tests', () => {
|
|||
|
||||
it('HTTP get request to get token endpoint', async () => {
|
||||
const http = new httpclient.HttpClient('actions/oidc-client')
|
||||
const res = await http.get(
|
||||
'https://ghactionsoidc.azurewebsites.net/.well-known/openid-configuration'
|
||||
)
|
||||
const res = await http.get(getTokenEndPoint())
|
||||
expect(res.message.statusCode).toBe(200)
|
||||
})
|
||||
|
||||
it('Get token endpoint', async () => {
|
||||
let url = await configvar.getIDTokenUrl()
|
||||
expect(url).toBeDefined()
|
||||
})
|
||||
|
||||
it('Fetch Id token', async () => {
|
||||
var id_token = main.getIDToken('helloworld')
|
||||
expect(id_token).toBeDefined()
|
||||
})
|
||||
})
|
||||
|
||||
/*test('HTTP get request to get token endpoint', async () => {
|
||||
expect(1).toBe(1)
|
||||
})*/
|
||||
|
|
|
@ -8,7 +8,7 @@ require('./sourcemap-register.js');module.exports =
|
|||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
exports.getRuntimeToken = exports.getIDTokenUrl = void 0;
|
||||
exports.getIDTokenUrl = exports.getRuntimeToken = void 0;
|
||||
const utils_1 = __webpack_require__(519);
|
||||
function getRuntimeToken() {
|
||||
const token = process.env['ACTIONS_RUNTIME_TOKEN'];
|
||||
|
@ -100,12 +100,8 @@ const core = __importStar(__webpack_require__(186));
|
|||
const actions_http_client = __importStar(__webpack_require__(925));
|
||||
const utils_1 = __webpack_require__(519);
|
||||
const config_variables_1 = __webpack_require__(463);
|
||||
function getIDToken(audience) {
|
||||
function postCall(id_token_url, audience) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
// New ID Token is requested from action service
|
||||
let id_token_url = config_variables_1.getIDTokenUrl();
|
||||
core.debug(`ID token url is ${id_token_url}`);
|
||||
const httpclient = utils_1.createHttpClient();
|
||||
if (httpclient === undefined) {
|
||||
throw new Error(`Failed to get Httpclient `);
|
||||
|
@ -116,13 +112,17 @@ function getIDToken(audience) {
|
|||
actions_http_client.MediaTypes.ApplicationJson;
|
||||
additionalHeaders[actions_http_client.Headers.Accept] =
|
||||
actions_http_client.MediaTypes.ApplicationJson;
|
||||
core.debug(`audience is ${(audience !== null) ? audience : "null"}`);
|
||||
const data = (audience !== null) ? JSON.stringify({ aud: audience }) : '';
|
||||
core.debug(`audience is ${audience !== null ? audience : 'null'}`);
|
||||
const data = audience !== null ? JSON.stringify({ aud: audience }) : '';
|
||||
const response = yield httpclient.post(id_token_url, data, additionalHeaders);
|
||||
if (!utils_1.isSuccessStatusCode(response.message.statusCode)) {
|
||||
throw new Error(`Failed to get ID Token. Error Code : ${response.message.statusCode} Error message : ${response.message.statusMessage}`);
|
||||
}
|
||||
const body = yield response.readBody();
|
||||
let body = yield response.readBody();
|
||||
return body;
|
||||
});
|
||||
}
|
||||
function parseJson(body) {
|
||||
const val = JSON.parse(body);
|
||||
let id_token = '';
|
||||
if ('value' in val) {
|
||||
|
@ -134,6 +134,16 @@ function getIDToken(audience) {
|
|||
core.debug(`id_token : ${id_token}`);
|
||||
return id_token;
|
||||
}
|
||||
function getIDToken(audience) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
try {
|
||||
// New ID Token is requested from action service
|
||||
let id_token_url = config_variables_1.getIDTokenUrl();
|
||||
core.debug(`ID token url is ${id_token_url}`);
|
||||
let body = yield postCall(id_token_url, audience);
|
||||
let id_token = parseJson(body);
|
||||
return id_token;
|
||||
}
|
||||
catch (error) {
|
||||
core.setFailed(error.message);
|
||||
return error.message;
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,6 +1,6 @@
|
|||
import {getApiVersion} from './utils'
|
||||
|
||||
function getRuntimeToken(): string {
|
||||
export function getRuntimeToken(){
|
||||
const token = process.env['ACTIONS_RUNTIME_TOKEN']
|
||||
if (!token) {
|
||||
throw new Error('Unable to get ACTIONS_RUNTIME_TOKEN env variable')
|
||||
|
@ -8,13 +8,10 @@ function getRuntimeToken(): string {
|
|||
return token
|
||||
}
|
||||
|
||||
function getIDTokenUrl(): string {
|
||||
export function getIDTokenUrl(){
|
||||
const 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 {getIDTokenUrl, getRuntimeToken}
|
|
@ -2,21 +2,19 @@ import {HttpClient} from '@actions/http-client'
|
|||
import {BearerCredentialHandler} from '@actions/http-client/auth'
|
||||
import {getRuntimeToken} from './config-variables'
|
||||
|
||||
function isSuccessStatusCode(statusCode?: number): boolean {
|
||||
export function isSuccessStatusCode(statusCode?: number): boolean {
|
||||
if (!statusCode) {
|
||||
return false
|
||||
}
|
||||
return statusCode >= 200 && statusCode < 300
|
||||
}
|
||||
|
||||
function createHttpClient(): HttpClient {
|
||||
export function createHttpClient(): HttpClient {
|
||||
return new HttpClient('actions/oidc-client', [
|
||||
new BearerCredentialHandler(getRuntimeToken())
|
||||
])
|
||||
}
|
||||
|
||||
function getApiVersion(): string {
|
||||
export function getApiVersion(): string {
|
||||
return '2.0'
|
||||
}
|
||||
|
||||
export {isSuccessStatusCode,createHttpClient,getApiVersion}
|
|
@ -4,13 +4,10 @@ import {IHeaders} from '@actions/http-client/interfaces'
|
|||
import {createHttpClient, isSuccessStatusCode} from './internal/utils'
|
||||
import {getIDTokenUrl} from './internal/config-variables'
|
||||
|
||||
export async function getIDToken(audience: string): Promise<string> {
|
||||
try {
|
||||
// New ID Token is requested from action service
|
||||
let id_token_url: string = getIDTokenUrl()
|
||||
|
||||
core.debug(`ID token url is ${id_token_url}`)
|
||||
|
||||
async function postCall(
|
||||
id_token_url: string,
|
||||
audience: string
|
||||
): Promise<string> {
|
||||
const httpclient = createHttpClient()
|
||||
if (httpclient === undefined) {
|
||||
throw new Error(`Failed to get Httpclient `)
|
||||
|
@ -26,31 +23,41 @@ export async function getIDToken(audience: string): Promise<string> {
|
|||
|
||||
core.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
|
||||
)
|
||||
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()
|
||||
|
||||
const body: string = await response.readBody()
|
||||
return body
|
||||
}
|
||||
|
||||
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')
|
||||
}
|
||||
|
||||
core.debug(`id_token : ${id_token}`)
|
||||
return id_token
|
||||
}
|
||||
|
||||
export async function getIDToken(audience: string): Promise<string> {
|
||||
try {
|
||||
// New ID Token is requested from action service
|
||||
let id_token_url: string = getIDTokenUrl()
|
||||
|
||||
core.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) {
|
||||
|
|
Loading…
Reference in New Issue