generated from sergiodxa/remix-auth-strategy-template
-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade
@oslojs/oauth2
dependency (#108)
Upgrade the doc to use the latest version of Oslo, copy parts of old versions of Oslo to this library to keep the compatibility while using the new version of Oslo
- Loading branch information
Showing
10 changed files
with
328 additions
and
53 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 |
---|---|---|
|
@@ -3,16 +3,8 @@ | |
"version": "2.1.0", | ||
"description": "A strategy to use and implement OAuth2 framework for authentication with federated services like Google, Facebook, GitHub, etc.", | ||
"license": "MIT", | ||
"funding": [ | ||
"https://github.com/sponsors/sergiodxa" | ||
], | ||
"keywords": [ | ||
"remix", | ||
"remix-auth", | ||
"auth", | ||
"authentication", | ||
"strategy" | ||
], | ||
"funding": ["https://github.com/sponsors/sergiodxa"], | ||
"keywords": ["remix", "remix-auth", "auth", "authentication", "strategy"], | ||
"author": { | ||
"name": "Sergio Xalambrí", | ||
"email": "[email protected]", | ||
|
@@ -31,23 +23,22 @@ | |
"typecheck": "tsc --noEmit", | ||
"quality": "biome check .", | ||
"quality:fix": "biome check . --apply-unsafe", | ||
"exports": "bun run ./scripts/exports.ts" | ||
"exports": "bun run ./scripts/exports.ts", | ||
"unused": "knip" | ||
}, | ||
"sideEffects": false, | ||
"type": "module", | ||
"engines": { | ||
"node": "^18.0.0 || ^20.0.0 || >=20.0.0" | ||
}, | ||
"files": [ | ||
"build", | ||
"package.json", | ||
"README.md" | ||
], | ||
"files": ["build", "package.json", "README.md"], | ||
"exports": { | ||
".": "./build/index.js", | ||
"./package.json": "./package.json" | ||
}, | ||
"dependencies": { | ||
"@oslojs/crypto": "^0.6.2", | ||
"@oslojs/encoding": "^0.4.1", | ||
"@oslojs/oauth2": "^0.5.0", | ||
"debug": "^4.3.4" | ||
}, | ||
|
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* A lot of the code here was originally implemented by @pilcrowOnPaper for a | ||
* previous version of `@oslojs/oauth2`, as Pilcrow decided to change the | ||
* direction of the library to focus on response parsing, I decided to copy the | ||
* old code and adapt it to the new structure of the library. | ||
*/ | ||
import { sha256 } from "@oslojs/crypto/sha2"; | ||
import { encodeBase64urlNoPadding } from "@oslojs/encoding"; | ||
|
||
export namespace AuthorizationCode { | ||
export class AuthorizationURL extends URL { | ||
constructor(authorizationEndpoint: string, clientId: string) { | ||
super(authorizationEndpoint); | ||
this.searchParams.set("response_type", "code"); | ||
this.searchParams.set("client_id", clientId); | ||
} | ||
|
||
public setRedirectURI(redirectURI: string): void { | ||
this.searchParams.set("redirect_uri", redirectURI); | ||
} | ||
|
||
public addScopes(...scopes: string[]): void { | ||
if (scopes.length < 1) { | ||
return; | ||
} | ||
let scopeValue = scopes.join(" "); | ||
const existingScopes = this.searchParams.get("scope"); | ||
if (existingScopes !== null) scopeValue = ` ${existingScopes}`; | ||
this.searchParams.set("scope", scopeValue); | ||
} | ||
|
||
public setState(state: string): void { | ||
this.searchParams.set("state", state); | ||
} | ||
|
||
public setS256CodeChallenge(codeVerifier: string): void { | ||
const codeChallengeBytes = sha256(new TextEncoder().encode(codeVerifier)); | ||
const codeChallenge = encodeBase64urlNoPadding(codeChallengeBytes); | ||
this.searchParams.set("code_challenge", codeChallenge); | ||
this.searchParams.set("code_challenge_method", "S256"); | ||
} | ||
|
||
public setPlainCodeChallenge(codeVerifier: string): void { | ||
this.searchParams.set("code_challenge", codeVerifier); | ||
this.searchParams.set("code_challenge_method", "plain"); | ||
} | ||
} | ||
} |
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,21 @@ | ||
/** | ||
* A lot of the code here was originally implemented by @pilcrowOnPaper for a | ||
* previous version of `@oslojs/oauth2`, as Pilcrow decided to change the | ||
* direction of the library to focus on response parsing, I decided to copy the | ||
* old code and adapt it to the new structure of the library. | ||
*/ | ||
import { encodeBase64urlNoPadding } from "@oslojs/encoding"; | ||
|
||
export namespace Generator { | ||
export function codeVerifier(): string { | ||
const randomValues = new Uint8Array(32); | ||
crypto.getRandomValues(randomValues); | ||
return encodeBase64urlNoPadding(randomValues); | ||
} | ||
|
||
export function state(): string { | ||
const randomValues = new Uint8Array(32); | ||
crypto.getRandomValues(randomValues); | ||
return encodeBase64urlNoPadding(randomValues); | ||
} | ||
} |
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,76 @@ | ||
/** | ||
* A lot of the code here was originally implemented by @pilcrowOnPaper for a | ||
* previous version of `@oslojs/oauth2`, as Pilcrow decided to change the | ||
* direction of the library to focus on response parsing, I decided to copy the | ||
* old code and adapt it to the new structure of the library. | ||
*/ | ||
import { encodeBase64 } from "@oslojs/encoding"; | ||
|
||
export namespace OAuth2Request { | ||
export abstract class Context { | ||
public method: string; | ||
public body = new URLSearchParams(); | ||
public headers = new Headers(); | ||
|
||
constructor(method: string) { | ||
this.method = method; | ||
this.headers.set("Content-Type", "application/x-www-form-urlencoded"); | ||
this.headers.set("Accept", "application/json"); | ||
this.headers.set("User-Agent", "oslo"); | ||
} | ||
|
||
public setClientId(clientId: string): void { | ||
this.body.set("client_id", clientId); | ||
} | ||
|
||
public authenticateWithRequestBody( | ||
clientId: string, | ||
clientSecret: string, | ||
): void { | ||
this.setClientId(clientId); | ||
this.body.set("client_secret", clientSecret); | ||
} | ||
|
||
public authenticateWithHTTPBasicAuth( | ||
clientId: string, | ||
clientSecret: string, | ||
): void { | ||
const authorizationHeader = `Basic ${encodeBase64( | ||
new TextEncoder().encode(`${clientId}:${clientSecret}`), | ||
)}`; | ||
this.headers.set("Authorization", authorizationHeader); | ||
} | ||
|
||
toRequest(url: ConstructorParameters<URL>["0"]) { | ||
return new Request(url, { | ||
method: this.method, | ||
body: this.body, | ||
headers: this.headers, | ||
}); | ||
} | ||
} | ||
|
||
// biome-ignore lint/suspicious/noShadowRestrictedNames: It's namespaced | ||
export class Error extends globalThis.Error { | ||
public request: Request; | ||
public context: OAuth2Request.Context; | ||
public description: string | null; | ||
public uri: string | null; | ||
public responseHeaders: Headers; | ||
|
||
constructor( | ||
message: string, | ||
request: Request, | ||
context: OAuth2Request.Context, | ||
responseHeaders: Headers, | ||
options?: { description?: string; uri?: string }, | ||
) { | ||
super(message); | ||
this.request = request; | ||
this.context = context; | ||
this.responseHeaders = responseHeaders; | ||
this.description = options?.description ?? null; | ||
this.uri = options?.uri ?? null; | ||
} | ||
} | ||
} |
Oops, something went wrong.