-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(github-auth): upgrade github client to latest
upgrade @octokit/rest to v^16.43.1 use @octokit/auth-basic and @octokit/auth-token for github auth fix #78
- Loading branch information
Showing
8 changed files
with
196 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
export const authenticatedUserToken = 'some-token'; | ||
export const createAuthorization = jest | ||
export const auth = jest | ||
.fn() | ||
.mockResolvedValue({ data: { token: authenticatedUserToken } }); | ||
.mockResolvedValue({ token: authenticatedUserToken }); | ||
|
||
export const createRepoForAuthenticatedUser = jest.fn(); | ||
|
||
const mock = jest.fn().mockImplementation(() => ({ | ||
export const Octokit = jest.fn().mockImplementation(() => ({ | ||
auth, | ||
repos: { createForAuthenticatedUser: createRepoForAuthenticatedUser }, | ||
oauthAuthorizations: { createAuthorization }, | ||
})); | ||
|
||
export default mock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,46 @@ | ||
import Octokit from '@octokit/rest'; | ||
import { Octokit } from '@octokit/rest'; | ||
import { createTokenAuth } from '@octokit/auth-token'; | ||
import { createBasicAuth } from '@octokit/auth-basic'; | ||
|
||
/** | ||
* Github class | ||
*/ | ||
export default class Github { | ||
/** | ||
* Github | ||
* @param {string} username github username | ||
* @param {string} password github password | ||
* @param {function} on2fa async function that should return | ||
* two-factor authentication pin (string) | ||
* Create a Github instance | ||
* @param {string} token Set token to login with auth-token | ||
* @param {string} username Set username to login with username and password | ||
* @param {string} password Set password to login with username and password | ||
* @param {Function} on2Fa Set on2Fa to support 2 factor authentication | ||
* @param {string} note Note description for the new token | ||
* @param {string} noteUrl Note url for the new token | ||
* @param {string[]} scopes Auth token scopes | ||
*/ | ||
constructor({ token, username, password, on2fa }) { | ||
constructor({ token, username, password, on2Fa, note, noteUrl, scopes }) { | ||
const authStrategy = token ? createTokenAuth : createBasicAuth; | ||
const auth = token || { | ||
username, | ||
password, | ||
on2Fa, | ||
token: { note, scopes, noteUrl }, | ||
}; | ||
|
||
this.githubClient = new Octokit({ | ||
auth: token || { | ||
username, | ||
password, | ||
on2fa, | ||
}, | ||
auth, | ||
authStrategy, | ||
}); | ||
} | ||
|
||
async authenticate() { | ||
const { token } = await this.githubClient.auth(); | ||
|
||
return token; | ||
} | ||
|
||
createRepository({ name, description }) { | ||
return this.githubClient.repos.createForAuthenticatedUser({ | ||
name, | ||
description, | ||
}); | ||
} | ||
|
||
async createToken(note, scopes) { | ||
const { | ||
data, | ||
} = await this.githubClient.oauthAuthorizations.createAuthorization({ | ||
note, | ||
scopes, | ||
}); | ||
|
||
return data.token; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.