-
Notifications
You must be signed in to change notification settings - Fork 13
Initial implementation #8
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
Closed
Closed
Changes from all commits
Commits
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vscode/ | ||
cov_profile/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"importMap": "./import_map.json", | ||
"compilerOptions": { | ||
"exactOptionalPropertyTypes": true, | ||
"lib": ["deno.ns", "dom"] | ||
} | ||
} |
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,11 @@ | ||
{ | ||
"imports": { | ||
"/": "./", | ||
"./": "./", | ||
"octokit": "https://cdn.skypack.dev/octokit?dts", | ||
"@octokit/types": "https://esm.sh/@octokit/types@6.34.0", | ||
"@octokit/auth-oauth-user": "https://esm.sh/@octokit/auth-oauth-user@1.3.0", | ||
"@octokit/oauth-authorization-url": "https://esm.sh/@octokit/oauth-authorization-url@4.3.3", | ||
"std/": "https://deno.land/std@0.143.0/" | ||
} | ||
} |
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,129 @@ | ||
import { endpoints } from "./endpoints.ts"; | ||
import { fetchOAuthApp } from "./fetch-oauth-app.ts"; | ||
import type { | ||
Auth, | ||
AuthenticatorMethods, | ||
AuthenticatorState, | ||
ClientTypes, | ||
OAuthAuthorizationUrlOptions, | ||
} from "./types.ts"; | ||
import { oauthAuthorizationUrl } from "@octokit/oauth-authorization-url"; | ||
|
||
export const auth = < | ||
ClientType extends ClientTypes, | ||
ExpirationEnabled extends boolean, | ||
>( | ||
state: AuthenticatorState<ClientType, ExpirationEnabled>, | ||
) => { | ||
type Command = AuthenticatorMethods<ClientType, ExpirationEnabled>; | ||
const authStore = state.authStore || undefined; | ||
const stateStore = state.stateStore || undefined; | ||
|
||
const fetchAuth = async ( | ||
type: keyof typeof endpoints, | ||
token: string | null, | ||
body: Record<string, unknown> | null, | ||
) => { | ||
let auth = (await fetchOAuthApp(state, type, token, body)) | ||
?.authentication || null; | ||
if (auth) auth = { ...(state.auth || {}), ...auth }; | ||
return await setAuth(auth); | ||
}; | ||
|
||
const setAuth = async ( | ||
auth: Auth<ClientType, ExpirationEnabled> | null = null, | ||
) => { | ||
await authStore?.set(auth); | ||
return (state.auth = auth); | ||
}; | ||
|
||
return async function auth( | ||
command: Command = { type: "getToken" }, | ||
): Promise<Auth<ClientType, ExpirationEnabled> | null> { | ||
const { type, ...commandOptions } = command; | ||
|
||
const url = new URL(state.location.href); | ||
const code = url.searchParams.get("code"); | ||
const newState = url.searchParams.get("state"); | ||
|
||
switch (type) { | ||
case "signIn": { | ||
await setAuth(); // clear local auth before redirecting | ||
const newState = Math.random().toString(36).substring(2); | ||
stateStore?.set(newState); | ||
const redirectUrl = oauthAuthorizationUrl<ClientType>({ | ||
clientType: state.clientType, | ||
clientId: state.clientId, | ||
redirectUrl: state.location.href, | ||
state: newState, | ||
...commandOptions, | ||
} as OAuthAuthorizationUrlOptions<ClientType>).url; | ||
state.location.href = redirectUrl; | ||
return null; | ||
} | ||
|
||
case "getToken": { | ||
if (!code || !newState) { | ||
state.auth ||= (await authStore?.get()) || null; | ||
if (!state.auth) return null; | ||
if ( | ||
// @ts-ignore better than a one-time assertion function | ||
!state.auth.expiresAt || new Date(state.auth.expiresAt) > new Date() | ||
) { | ||
return state.auth; | ||
} | ||
return await auth({ type: "renewToken" } as Command); | ||
} | ||
} | ||
|
||
/* falls through */ | ||
|
||
case "createToken": { | ||
if (!code || !newState) { | ||
throw Error('Both "code" & "state" parameters are required.'); | ||
} | ||
url.searchParams.delete("code"); | ||
url.searchParams.delete("state"); | ||
const redirectUrl = url.href; | ||
// @ts-ignore mock `window.history` in tests | ||
window.history.replaceState({}, "", redirectUrl); | ||
const oldState = (await stateStore?.get()); | ||
await stateStore?.set(null); | ||
if (stateStore && (newState != oldState)) { | ||
throw Error("State mismatch."); | ||
} | ||
return await fetchAuth("createToken", null, { | ||
state: newState, // TODO: this is unnecessary, update oauth-app | ||
code, | ||
redirectUrl, | ||
}); | ||
} | ||
|
||
case "checkToken": | ||
case "createScopedToken": | ||
case "resetToken": | ||
case "renewToken": | ||
case "deleteToken": | ||
case "deleteAuthorization": { | ||
let body: Record<string, unknown> | null = null; | ||
if (["POST", "PUT", "PATCH"].includes(endpoints[type]?.[0])) { | ||
const { type: _, ..._payload } = command as Record<string, unknown>; | ||
body = _payload; | ||
} | ||
if (type === "deleteToken" && command.offline) return await setAuth(); | ||
if (type === "renewToken") { | ||
if (state.auth) { | ||
const auth = state.auth as Auth<ClientType, true>; | ||
const renewableUntil = new Date(auth.refreshTokenExpiresAt); | ||
if (new Date() > renewableUntil) return await setAuth(); | ||
body!.refreshToken = auth.refreshToken; | ||
} | ||
} else state.auth = await auth(); | ||
if (!state.auth) throw Error("Unauthorized."); | ||
const { token } = state.auth; // TODO: does `renewToken` need token? | ||
if (type.startsWith("delete")) await setAuth(); | ||
return await fetchAuth(type, token, body); | ||
} | ||
} | ||
}; | ||
}; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please make sure to add
node_modules
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There will not be
node_modules
becausenode
andnpm
are not involved: instead of install dependencies, justdeno test
ordeno bundle
.Please let me know if this is acceptable.