-
-
Notifications
You must be signed in to change notification settings - Fork 30
Feature/authentication strategies #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
precious-void
wants to merge
10
commits into
MunifTanjim:master
Choose a base branch
from
precious-void:feature/authentication-strategies
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
908f137
Fixed eslint & cloned auth strategies from octokit
1eb49c9
Update
36a1e64
Clear changes
39ad05b
Added auth strategies to readme
5d63fd0
Remove renamings
6a28d06
Small validation
b3de5e9
:zap: Fixing circular auth hook call
3e5f51b
⚡️ Fixing request initialization
e10df78
✏️ Removed unnecessary code, fixed types export
5cba7cb
Bring authenticate plugin back from the hell
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -3,4 +3,4 @@ docs | |
lib | ||
|
||
# Dependency directories | ||
node_modules | ||
node_modules |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { getOAuthAccessToken } from './get-oauth-access-token' | ||
|
||
type Authentication = import('./types').Authentication | ||
type AuthState = import('./types').AuthPluginState | ||
|
||
export async function auth(authState: AuthState): Promise<Authentication | {}> { | ||
if (!authState.token) { | ||
const newToken = await getOAuthAccessToken(authState) | ||
authState.token = newToken | ||
} | ||
|
||
return authState.token ?? {} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import btoa from 'btoa-lite' | ||
import oAuth2Spec from './spec.json' | ||
import getOAuthRoutes from './routes' | ||
import { request as Request } from '../../../request' | ||
|
||
type AuthState = import('./types').AuthPluginState | ||
type EndpointOptions = import('./types').EndpointOptions | ||
|
||
const routes = getOAuthRoutes(oAuth2Spec).getToken | ||
export const getOAuthAccessToken = async (state: AuthState): Promise<any> => { | ||
const { auth: authState } = state | ||
const { grant_type: grantType } = authState | ||
const { accepts, url, method, grant_type } = routes[grantType] | ||
|
||
const endpointOptions: EndpointOptions = { | ||
accepts, | ||
url, | ||
method, | ||
headers: {}, | ||
request: {}, | ||
grant_type, | ||
} | ||
|
||
endpointOptions.headers!.authorization = | ||
authState.grant_type === 'bitbucketCloudJWTGrant' | ||
? `JWT ${btoa(`${authState.jwt_token}`)}` | ||
: `Basic ${btoa(`${authState.client_id}:${authState.client_secret}`)}` | ||
|
||
if (authState.grant_type === 'authorizationCodeGrant') { | ||
endpointOptions.code = authState.code | ||
} | ||
|
||
if (authState.grant_type === 'resourceOwnerPasswordCredentialsGrant') { | ||
endpointOptions.username = authState.username | ||
endpointOptions.password = authState.password | ||
} | ||
|
||
const response = await Request(endpointOptions) | ||
const { data } = response | ||
|
||
const newToken = { | ||
access_token: data.access_token, | ||
refresh_token: data.refresh_token, | ||
scopes: data.scopes.split(/\s/).filter(Boolean), | ||
} | ||
|
||
return newToken | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { auth } from './auth' | ||
|
||
type AuthState = import('./types').AuthPluginState | ||
type RequestOptions = import('./types').RequestOptions | ||
|
||
export async function beforeHook( | ||
authState: AuthState, | ||
requestOptions: RequestOptions | ||
): Promise<void> { | ||
if (!authState.token) { | ||
await auth(authState) | ||
} else if (authState.token.access_token) { | ||
requestOptions.headers.authorization = `Bearer ${authState.token.access_token}` | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { auth } from './auth' | ||
import { beforeHook } from './hook' | ||
import { validateOptions } from './validate-options' | ||
|
||
type APIClient = import('./types').APIClient | ||
type AuthPluginState = import('./types').AuthPluginState | ||
type Options = import('./types').Options | ||
|
||
const OAuth = (client: APIClient, clientOptions: Options): void => { | ||
if (!clientOptions.auth || clientOptions.authStrategy !== 'OAuth') return | ||
|
||
validateOptions(clientOptions.auth) | ||
|
||
const state: AuthPluginState = { | ||
authStrategy: clientOptions.authStrategy, | ||
auth: clientOptions.auth, | ||
} | ||
|
||
client.auth = auth.bind(null, state) | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
client.requestHook.before(beforeHook.bind(null, state)) | ||
} | ||
|
||
export default OAuth |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.