@codefresh-io/cf-git-providers
Advanced tools
Comparing version
@@ -1,6 +0,8 @@ | ||
import { Provider } from './providers/types'; | ||
export declare function getProviderByContext(gitContext: any, options?: { | ||
import { Provider, RefreshTokenHandler } from './providers/types'; | ||
export declare function getProviderByContext(gitContext: any, opt?: { | ||
insecure?: boolean; | ||
timeout?: number; | ||
refreshToken?: string; | ||
refreshTokenHandler?: RefreshTokenHandler; | ||
}): Promise<Provider>; | ||
export * from './providers/types'; |
@@ -38,6 +38,6 @@ "use strict"; | ||
}; | ||
async function getProviderByContext(gitContext, options) { | ||
async function getProviderByContext(gitContext, opt) { | ||
const providerName = gitContext.spec.type.replace('git.', ''); | ||
const providerOptions = lodash_1.get(gitContext, 'spec.data.auth', {}); | ||
lodash_1.assign(providerOptions, options); // add additional options | ||
lodash_1.assign(providerOptions, opt); // add additional options | ||
const ProviderClass = _getProviderModule(providerName); | ||
@@ -44,0 +44,0 @@ return new ProviderClass(providerOptions); |
@@ -1,2 +0,2 @@ | ||
import { Provider, Branch, Repository, Webhook, ProviderName, User, PullRequest, RepositoryPermission, PermissionScopes, GitAuth } from './types'; | ||
import { Provider, Branch, Repository, Webhook, ProviderName, User, PullRequest, RepositoryPermission, PermissionScopes, GitAuth, RefreshTokenHandler } from './types'; | ||
export default class Gitlab implements Provider { | ||
@@ -8,4 +8,6 @@ private baseUrl; | ||
private auth; | ||
private refreshTokenHandler?; | ||
constructor(opts: { | ||
password: string; | ||
refreshToken?: string; | ||
apiURL?: string; | ||
@@ -16,2 +18,3 @@ timeout?: number; | ||
apiVersion?: string; | ||
refreshTokenHandler?: RefreshTokenHandler; | ||
}); | ||
@@ -18,0 +21,0 @@ createRepository(opt: { |
@@ -133,2 +133,3 @@ "use strict"; | ||
this.timeout = opts.timeout || 10000; | ||
this.refreshTokenHandler = opts.refreshTokenHandler; | ||
// works for both oauth2 and private token authentication methods | ||
@@ -140,2 +141,3 @@ // eslint-disable-next-line @typescript-eslint/naming-convention | ||
password: opts.password, | ||
refreshToken: opts.refreshToken, | ||
}; | ||
@@ -190,2 +192,17 @@ if (this.baseUrl.startsWith('https') && (process.env.NODE_TLS_REJECT_UNAUTHORIZED === '0' || opts.insecure)) { | ||
return request_promise_1.default(requestOptions) | ||
.then(async (res) => { | ||
if (res.statusCode === 401 && this.refreshTokenHandler && this.auth.refreshToken) { | ||
const auth = await this.refreshTokenHandler(this.auth.refreshToken); | ||
if (!auth) { | ||
throw new Error(`Refresh token error`); | ||
} | ||
this.auth.password = auth.accessToken; | ||
this.auth.refreshToken = auth.refreshToken; | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
this.authenticationHeader = auth.accessToken ? { Authorization: `Bearer ${auth.accessToken}` } : {}; | ||
requestOptions.headers = lodash_1.merge(opts.noAuth ? {} : this.authenticationHeader, opts.headers || {}); | ||
return request_promise_1.default(requestOptions); | ||
} | ||
return Promise.resolve(res); | ||
}) | ||
.then((res) => { | ||
@@ -459,3 +476,3 @@ logger.debug(`request: ${requestStr} status: ${res.statusCode}`); | ||
getAuth() { | ||
return this.auth; | ||
return lodash_1.omit(this.auth, 'refreshToken'); | ||
} | ||
@@ -462,0 +479,0 @@ } |
@@ -0,1 +1,6 @@ | ||
export declare type RefreshTokenHandlerReturn = Promise<{ | ||
accessToken: string; | ||
refreshToken: string; | ||
} | undefined>; | ||
export declare type RefreshTokenHandler = (refreshToken: string) => RefreshTokenHandlerReturn; | ||
export declare type ProviderConstructor = new (opt: { | ||
@@ -13,2 +18,3 @@ password: string; | ||
insecure?: boolean; | ||
refreshTokenHandler?: RefreshTokenHandler; | ||
}) => Provider; | ||
@@ -15,0 +21,0 @@ export declare type ProviderName = 'github' | 'bitbucket' | 'gitlab' | 'bitbucket-server'; |
{ | ||
"name": "@codefresh-io/cf-git-providers", | ||
"version": "0.4.13", | ||
"version": "0.4.14", | ||
"description": "An NPM module/CLI for interacting with various git providers", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
248245
0.98%3761
0.75%