-
Notifications
You must be signed in to change notification settings - Fork 141
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
Add OAuth2 scope support #156
Open
lionello
wants to merge
3
commits into
openauthjs:master
Choose a base branch
from
DefangLabs:lio/add-scope
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
3 commits
Select commit
Hold shift + click to select a range
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 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,5 @@ | ||
--- | ||
"@openauthjs/openauth": minor | ||
--- | ||
|
||
Add support for OAuth2 scope |
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 |
---|---|---|
|
@@ -124,12 +124,12 @@ | |
* | ||
* @packageDocumentation | ||
*/ | ||
import { Provider, ProviderOptions } from "./provider/provider.js" | ||
import { SubjectPayload, SubjectSchema } from "./subject.js" | ||
import { Hono } from "hono/tiny" | ||
import { handle as awsHandle } from "hono/aws-lambda" | ||
import { Context } from "hono" | ||
import { handle as awsHandle } from "hono/aws-lambda" | ||
import { deleteCookie, getCookie, setCookie } from "hono/cookie" | ||
import { Hono } from "hono/tiny" | ||
import { Provider, ProviderOptions } from "./provider/provider.js" | ||
import { SubjectPayload, SubjectSchema } from "./subject.js" | ||
|
||
/** | ||
* Sets the subject payload in the JWT token and returns the response. | ||
|
@@ -171,6 +171,7 @@ export interface AuthorizationState { | |
state: string | ||
client_id: string | ||
audience?: string | ||
scopes?: string[] | ||
pkce?: { | ||
challenge: string | ||
method: "S256" | ||
|
@@ -184,18 +185,23 @@ export type Prettify<T> = { | |
[K in keyof T]: T[K] | ||
} & {} | ||
|
||
import { cors } from "hono/cors" | ||
import { compactDecrypt, CompactEncrypt, SignJWT } from "jose" | ||
import { | ||
MissingParameterError, | ||
OauthError, | ||
UnauthorizedClientError, | ||
UnknownStateError, | ||
} from "./error.js" | ||
import { compactDecrypt, CompactEncrypt, SignJWT } from "jose" | ||
import { Storage, StorageAdapter } from "./storage/storage.js" | ||
import { encryptionKeys, legacySigningKeys, signingKeys } from "./keys.js" | ||
import { validatePKCE } from "./pkce.js" | ||
import { parseScopes, validateScopes } from "./scopes.js" | ||
import { DynamoStorage } from "./storage/dynamo.js" | ||
import { MemoryStorage } from "./storage/memory.js" | ||
import { Storage, StorageAdapter } from "./storage/storage.js" | ||
import { Select } from "./ui/select.js" | ||
import { setTheme, Theme } from "./ui/theme.js" | ||
import { isDomainMatch } from "./util.js" | ||
import { getRelativeUrl, isDomainMatch } from "./util.js" | ||
import { DynamoStorage } from "./storage/dynamo.js" | ||
import { MemoryStorage } from "./storage/memory.js" | ||
|
@@ -279,6 +285,17 @@ export interface IssuerInput< | |
* ``` | ||
*/ | ||
providers: Providers | ||
/** | ||
* Array containing a list of the OAuth 2.0 [RFC6749] "scope" values that this authorization server advertises. | ||
* | ||
* @example | ||
* ```ts | ||
* { | ||
* scopes_supported: ["read", "write"] | ||
* } | ||
* ``` | ||
*/ | ||
scopes_supported?: string[] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would also be a good place to add |
||
/** | ||
* The theme you want to use for the UI. | ||
* | ||
|
@@ -525,6 +542,7 @@ export function issuer< | |
type: type as string, | ||
properties, | ||
clientID: authorization.client_id, | ||
scopes: authorization.scopes, | ||
ttl: { | ||
access: subjectOpts?.ttl?.access ?? ttlAccess, | ||
refresh: subjectOpts?.ttl?.refresh ?? ttlRefresh, | ||
|
@@ -550,6 +568,7 @@ export function issuer< | |
redirectURI: authorization.redirect_uri, | ||
clientID: authorization.client_id, | ||
pkce: authorization.pkce, | ||
scopes: authorization.scopes, | ||
ttl: { | ||
access: subjectOpts?.ttl?.access ?? ttlAccess, | ||
refresh: subjectOpts?.ttl?.refresh ?? ttlRefresh, | ||
|
@@ -654,6 +673,7 @@ export function issuer< | |
} | ||
timeUsed?: number | ||
nextToken?: string | ||
scopes?: string[] | ||
}, | ||
opts?: { | ||
generateRefreshToken?: boolean | ||
|
@@ -687,6 +707,7 @@ export function issuer< | |
aud: value.clientID, | ||
iss: issuer(ctx), | ||
sub: value.subject, | ||
scopes: value.scopes, | ||
}) | ||
.setExpirationTime( | ||
Math.floor((value.timeUsed ?? Date.now()) / 1000 + value.ttl.access), | ||
|
@@ -774,6 +795,7 @@ export function issuer< | |
token_endpoint: `${iss}/token`, | ||
jwks_uri: `${iss}/.well-known/jwks.json`, | ||
response_types_supported: ["code", "token"], | ||
scopes_supported: input.scopes_supported, | ||
}) | ||
}, | ||
) | ||
|
@@ -789,6 +811,7 @@ export function issuer< | |
async (c) => { | ||
const form = await c.req.formData() | ||
const grantType = form.get("grant_type") | ||
const scope = form.get("scope") as string | null | ||
|
||
if (grantType === "authorization_code") { | ||
const code = form.get("code") | ||
|
@@ -807,6 +830,7 @@ export function issuer< | |
clientID: string | ||
redirectURI: string | ||
subject: string | ||
scopes?: string[] | ||
ttl: { | ||
access: number | ||
refresh: number | ||
|
@@ -870,10 +894,12 @@ export function issuer< | |
) | ||
} | ||
} | ||
payload.scopes = validateScopes(scope, payload.scopes) | ||
const tokens = await generateTokens(c, payload) | ||
return c.json({ | ||
access_token: tokens.access, | ||
refresh_token: tokens.refresh, | ||
scope: payload.scopes?.join(" "), | ||
}) | ||
} | ||
|
||
|
@@ -896,6 +922,7 @@ export function issuer< | |
properties: any | ||
clientID: string | ||
subject: string | ||
scopes?: string[] | ||
ttl: { | ||
access: number | ||
refresh: number | ||
|
@@ -935,12 +962,14 @@ export function issuer< | |
400, | ||
) | ||
} | ||
payload.scopes = validateScopes(scope, payload.scopes) | ||
const tokens = await generateTokens(c, payload, { | ||
generateRefreshToken, | ||
}) | ||
return c.json({ | ||
access_token: tokens.access, | ||
refresh_token: tokens.refresh, | ||
scope: payload.scopes?.join(" "), | ||
}) | ||
} | ||
|
||
|
@@ -976,6 +1005,7 @@ export function issuer< | |
opts?.subject || (await resolveSubject(type, properties)), | ||
properties, | ||
clientID: clientID.toString(), | ||
scopes: parseScopes(scope), | ||
ttl: { | ||
access: opts?.ttl?.access ?? ttlAccess, | ||
refresh: opts?.ttl?.refresh ?? ttlRefresh, | ||
|
@@ -1008,12 +1038,14 @@ export function issuer< | |
const audience = c.req.query("audience") | ||
const code_challenge = c.req.query("code_challenge") | ||
const code_challenge_method = c.req.query("code_challenge_method") | ||
const scope = c.req.query("scope") | ||
const authorization: AuthorizationState = { | ||
response_type, | ||
redirect_uri, | ||
state, | ||
client_id, | ||
audience, | ||
scopes: parseScopes(scope), | ||
pkce: | ||
code_challenge && code_challenge_method | ||
? { | ||
|
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function parseScopes(scope: string | null | undefined) { | ||
return scope?.split(" ").filter((s) => s) | ||
} | ||
|
||
export function validateScopes(tokenReq?: string | null, authorizeReq?: string[]) { | ||
if (!authorizeReq?.length || tokenReq === null || tokenReq === undefined) { | ||
return authorizeReq | ||
} | ||
return [...new Set(parseScopes(tokenReq)).intersection(new Set(authorizeReq))] | ||
} |
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.
These got reordered because of
Organize Imports
. Let me know if you want me to revert this part.