feat: add @actions/cache

This commit is contained in:
xHyroM 2022-07-12 09:00:22 +02:00
parent b15fb7d098
commit 16e8c96a41
1932 changed files with 261172 additions and 10 deletions

View file

@ -0,0 +1,89 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import { HttpHeaders } from "../httpHeaders";
import { WebResourceLike } from "../webResource";
import { ServiceClientCredentials } from "./serviceClientCredentials";
/**
* @interface ApiKeyCredentialOptions
* Describes the options to be provided while creating an instance of ApiKeyCredentials
*/
export interface ApiKeyCredentialOptions {
/**
* A key value pair of the header parameters that need to be applied to the request.
*/
inHeader?: { [x: string]: any };
/**
* A key value pair of the query parameters that need to be applied to the request.
*/
inQuery?: { [x: string]: any };
}
/**
* Authenticates to a service using an API key.
*/
export class ApiKeyCredentials implements ServiceClientCredentials {
/**
* A key value pair of the header parameters that need to be applied to the request.
*/
private readonly inHeader?: { [x: string]: any };
/**
* A key value pair of the query parameters that need to be applied to the request.
*/
private readonly inQuery?: { [x: string]: any };
/**
* @constructor
* @param {object} options Specifies the options to be provided for auth. Either header or query needs to be provided.
*/
constructor(options: ApiKeyCredentialOptions) {
if (!options || (options && !options.inHeader && !options.inQuery)) {
throw new Error(
`options cannot be null or undefined. Either "inHeader" or "inQuery" property of the options object needs to be provided.`
);
}
this.inHeader = options.inHeader;
this.inQuery = options.inQuery;
}
/**
* Signs a request with the values provided in the inHeader and inQuery parameter.
*
* @param {WebResource} webResource The WebResource to be signed.
* @returns {Promise<WebResource>} The signed request object.
*/
signRequest(webResource: WebResourceLike): Promise<WebResourceLike> {
if (!webResource) {
return Promise.reject(
new Error(`webResource cannot be null or undefined and must be of type "object".`)
);
}
if (this.inHeader) {
if (!webResource.headers) {
webResource.headers = new HttpHeaders();
}
for (const headerName in this.inHeader) {
webResource.headers.set(headerName, this.inHeader[headerName]);
}
}
if (this.inQuery) {
if (!webResource.url) {
return Promise.reject(new Error(`url cannot be null in the request object.`));
}
if (webResource.url.indexOf("?") < 0) {
webResource.url += "?";
}
for (const key in this.inQuery) {
if (!webResource.url.endsWith("?")) {
webResource.url += "&";
}
webResource.url += `${key}=${this.inQuery[key]}`;
}
}
return Promise.resolve(webResource);
}
}

View file

@ -0,0 +1,60 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import { ServiceClientCredentials } from "./serviceClientCredentials";
import { Constants as MSRestConstants } from "../util/constants";
import { WebResource } from "../webResource";
import { TokenCredential } from "@azure/core-auth";
import { TokenResponse } from "./tokenResponse";
const DEFAULT_AUTHORIZATION_SCHEME = "Bearer";
/**
* Resource manager endpoints to match in order to specify a valid scope to the AzureIdentityCredentialAdapter.
*/
export const azureResourceManagerEndpoints = [
"https://management.windows.net",
"https://management.chinacloudapi.cn",
"https://management.usgovcloudapi.net",
"https://management.cloudapi.de",
];
/**
* This class provides a simple extension to use {@link TokenCredential} from `@azure/identity` library to
* use with legacy Azure SDKs that accept {@link ServiceClientCredentials} family of credentials for authentication.
*/
export class AzureIdentityCredentialAdapter implements ServiceClientCredentials {
private azureTokenCredential: TokenCredential;
private scopes: string | string[];
constructor(
azureTokenCredential: TokenCredential,
scopes: string | string[] = "https://management.azure.com/.default"
) {
this.azureTokenCredential = azureTokenCredential;
this.scopes = scopes;
}
public async getToken(): Promise<TokenResponse> {
const accessToken = await this.azureTokenCredential.getToken(this.scopes);
if (accessToken !== null) {
const result: TokenResponse = {
accessToken: accessToken.token,
tokenType: DEFAULT_AUTHORIZATION_SCHEME,
expiresOn: accessToken.expiresOnTimestamp,
};
return result;
} else {
throw new Error("Could find token for scope");
}
}
public async signRequest(webResource: WebResource) {
const tokenResponse = await this.getToken();
webResource.headers.set(
MSRestConstants.HeaderConstants.AUTHORIZATION,
`${tokenResponse.tokenType} ${tokenResponse.accessToken}`
);
return Promise.resolve(webResource);
}
}

View file

@ -0,0 +1,54 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import { HttpHeaders } from "../httpHeaders";
import * as base64 from "../util/base64";
import { Constants } from "../util/constants";
import { WebResourceLike } from "../webResource";
import { ServiceClientCredentials } from "./serviceClientCredentials";
const HeaderConstants = Constants.HeaderConstants;
const DEFAULT_AUTHORIZATION_SCHEME = "Basic";
export class BasicAuthenticationCredentials implements ServiceClientCredentials {
userName: string;
password: string;
authorizationScheme: string = DEFAULT_AUTHORIZATION_SCHEME;
/**
* Creates a new BasicAuthenticationCredentials object.
*
* @constructor
* @param {string} userName User name.
* @param {string} password Password.
* @param {string} [authorizationScheme] The authorization scheme.
*/
constructor(
userName: string,
password: string,
authorizationScheme: string = DEFAULT_AUTHORIZATION_SCHEME
) {
if (userName === null || userName === undefined || typeof userName.valueOf() !== "string") {
throw new Error("userName cannot be null or undefined and must be of type string.");
}
if (password === null || password === undefined || typeof password.valueOf() !== "string") {
throw new Error("password cannot be null or undefined and must be of type string.");
}
this.userName = userName;
this.password = password;
this.authorizationScheme = authorizationScheme;
}
/**
* Signs a request with the Authentication header.
*
* @param {WebResourceLike} webResource The WebResourceLike to be signed.
* @returns {Promise<WebResourceLike>} The signed request object.
*/
signRequest(webResource: WebResourceLike) {
const credentials = `${this.userName}:${this.password}`;
const encodedCredentials = `${this.authorizationScheme} ${base64.encodeString(credentials)}`;
if (!webResource.headers) webResource.headers = new HttpHeaders();
webResource.headers.set(HeaderConstants.AUTHORIZATION, encodedCredentials);
return Promise.resolve(webResource);
}
}

View file

@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
export type Authenticator = (challenge: object) => Promise<string>;

View file

@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import { ApiKeyCredentials, ApiKeyCredentialOptions } from "./apiKeyCredentials";
export class DomainCredentials extends ApiKeyCredentials {
/**
* Creates a new EventGrid DomainCredentials object.
*
* @constructor
* @param {string} domainKey The EventGrid domain key
*/
constructor(domainKey: string) {
if (!domainKey || (domainKey && typeof domainKey !== "string")) {
throw new Error("domainKey cannot be null or undefined and must be of type string.");
}
const options: ApiKeyCredentialOptions = {
inHeader: {
"aeg-sas-key": domainKey,
},
};
super(options);
}
}

View file

@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import { WebResourceLike } from "../webResource";
export interface ServiceClientCredentials {
/**
* Signs a request with the Authentication header.
*
* @param {WebResourceLike} webResource The WebResourceLike/request to be signed.
* @returns {Promise<WebResourceLike>} The signed request object;
*/
signRequest(webResource: WebResourceLike): Promise<WebResourceLike>;
}

View file

@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import { HttpHeaders } from "../httpHeaders";
import { Constants } from "../util/constants";
import { WebResourceLike } from "../webResource";
import { ServiceClientCredentials } from "./serviceClientCredentials";
const HeaderConstants = Constants.HeaderConstants;
const DEFAULT_AUTHORIZATION_SCHEME = "Bearer";
/**
* A credentials object that uses a token string and a authorzation scheme to authenticate.
*/
export class TokenCredentials implements ServiceClientCredentials {
token: string;
authorizationScheme: string = DEFAULT_AUTHORIZATION_SCHEME;
/**
* Creates a new TokenCredentials object.
*
* @constructor
* @param {string} token The token.
* @param {string} [authorizationScheme] The authorization scheme.
*/
constructor(token: string, authorizationScheme: string = DEFAULT_AUTHORIZATION_SCHEME) {
if (!token) {
throw new Error("token cannot be null or undefined.");
}
this.token = token;
this.authorizationScheme = authorizationScheme;
}
/**
* Signs a request with the Authentication header.
*
* @param {WebResourceLike} webResource The WebResourceLike to be signed.
* @return {Promise<WebResourceLike>} The signed request object.
*/
signRequest(webResource: WebResourceLike) {
if (!webResource.headers) webResource.headers = new HttpHeaders();
webResource.headers.set(
HeaderConstants.AUTHORIZATION,
`${this.authorizationScheme} ${this.token}`
);
return Promise.resolve(webResource);
}
}

View file

@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
/**
* TokenResponse is defined in `@azure/ms-rest-nodeauth` and is copied here to not
* add an unnecessary dependency.
*/
export interface TokenResponse {
readonly tokenType: string;
readonly accessToken: string;
readonly [x: string]: any;
}

View file

@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import { ApiKeyCredentials, ApiKeyCredentialOptions } from "./apiKeyCredentials";
export class TopicCredentials extends ApiKeyCredentials {
/**
* Creates a new EventGrid TopicCredentials object.
*
* @constructor
* @param {string} topicKey The EventGrid topic key
*/
constructor(topicKey: string) {
if (!topicKey || (topicKey && typeof topicKey !== "string")) {
throw new Error("topicKey cannot be null or undefined and must be of type string.");
}
const options: ApiKeyCredentialOptions = {
inHeader: {
"aeg-sas-key": topicKey,
},
};
super(options);
}
}