-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0a18c9
commit 29deb3b
Showing
22 changed files
with
605 additions
and
32 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,5 +1,14 @@ | ||
# @authhero/demo | ||
|
||
## 0.5.11 | ||
|
||
### Patch Changes | ||
|
||
- Updated dependencies | ||
- Updated dependencies [a0a18c9] | ||
- [email protected] | ||
- @authhero/kysely-adapter@0.26.1 | ||
|
||
## 0.5.10 | ||
|
||
### Patch Changes | ||
|
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
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,97 @@ | ||
import { Context } from "hono"; | ||
import { AuthParams, Client, LogTypes } from "@authhero/adapter-interfaces"; | ||
import { HTTPException } from "hono/http-exception"; | ||
import { createLogMessage } from "../utils/create-log-message"; | ||
import { nanoid } from "nanoid"; | ||
import { getClientInfo } from "../utils/client-info"; | ||
import { Bindings, Variables } from "../types"; | ||
import { | ||
OAUTH2_CODE_EXPIRES_IN_SECONDS, | ||
UNIVERSAL_AUTH_SESSION_EXPIRES_IN_SECONDS, | ||
} from "../constants"; | ||
import { strategies } from "../strategies"; | ||
import { setSearchParams } from "../utils/url"; | ||
|
||
export async function connectionAuth( | ||
ctx: Context<{ Bindings: Bindings; Variables: Variables }>, | ||
client: Client, | ||
connectionName: string, | ||
authParams: AuthParams, | ||
) { | ||
if (!authParams.state) { | ||
throw new HTTPException(400, { message: "State not found" }); | ||
} | ||
|
||
const connection = client.connections.find((p) => p.name === connectionName); | ||
|
||
if (!connection) { | ||
ctx.set("client_id", client.id); | ||
const log = createLogMessage(ctx, { | ||
type: LogTypes.FAILED_LOGIN, | ||
description: "Connection not found", | ||
}); | ||
await ctx.env.data.logs.create(client.tenant.id, log); | ||
|
||
throw new HTTPException(403, { message: "Connection Not Found" }); | ||
} | ||
|
||
let loginSession = await ctx.env.data.logins.get( | ||
client.tenant.id, | ||
authParams.state, | ||
); | ||
|
||
if (!loginSession) { | ||
loginSession = await ctx.env.data.logins.create(client.tenant.id, { | ||
expires_at: new Date( | ||
Date.now() + UNIVERSAL_AUTH_SESSION_EXPIRES_IN_SECONDS * 1000, | ||
).toISOString(), | ||
authParams, | ||
...getClientInfo(ctx.req), | ||
}); | ||
} | ||
|
||
const options = connection.options || {}; | ||
|
||
const strategy = strategies[connection.strategy]; | ||
|
||
if (strategy) { | ||
const result = await strategy.getRedirect(ctx, connection); | ||
|
||
await ctx.env.data.codes.create(client.tenant.id, { | ||
login_id: loginSession.login_id, | ||
code_id: result.code, | ||
code_type: "oauth2_state", | ||
connection_id: connection.id, | ||
code_verifier: result.codeVerifier, | ||
expires_at: new Date( | ||
Date.now() + OAUTH2_CODE_EXPIRES_IN_SECONDS * 1000, | ||
).toISOString(), | ||
}); | ||
|
||
return ctx.redirect(result.redirectUrl); | ||
} | ||
|
||
// This the legacy version | ||
const auth2State = await ctx.env.data.codes.create(client.tenant.id, { | ||
login_id: loginSession.login_id, | ||
code_id: nanoid(), | ||
code_type: "oauth2_state", | ||
connection_id: connection.id, | ||
expires_at: new Date( | ||
Date.now() + OAUTH2_CODE_EXPIRES_IN_SECONDS * 1000, | ||
).toISOString(), | ||
}); | ||
|
||
const oauthLoginUrl = new URL(options.authorization_endpoint!); | ||
|
||
setSearchParams(oauthLoginUrl, { | ||
scope: options.scope, | ||
client_id: options.client_id, | ||
redirect_uri: `${ctx.env.ISSUER}callback`, | ||
response_type: connection.response_type, | ||
response_mode: connection.response_mode, | ||
state: auth2State.code_id, | ||
}); | ||
|
||
return ctx.redirect(oauthLoginUrl.href); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Apple } from "arctic"; | ||
import { Context } from "hono"; | ||
import { Connection } from "@authhero/adapter-interfaces"; | ||
import { nanoid } from "nanoid"; | ||
import { Bindings, Variables } from "../types"; | ||
import { parseJWT } from "oslo/jwt"; | ||
import { idTokenSchema } from "../types/IdToken"; | ||
|
||
function getAppleOptions(connection: Connection) { | ||
const { options } = connection; | ||
|
||
if ( | ||
!options || | ||
!options.client_id || | ||
!options.team_id || | ||
!options.kid || | ||
!options.app_secret | ||
) { | ||
throw new Error("Missing required Apple authentication parameters"); | ||
} | ||
|
||
// Use a secure buffer to handle private key | ||
const privateKeyBuffer = Buffer.from(options.app_secret, "utf-8"); | ||
const cleanedKey = privateKeyBuffer | ||
.toString() | ||
.replace(/-----BEGIN PRIVATE KEY-----|-----END PRIVATE KEY-----|\s/g, ""); | ||
const keyArray = Uint8Array.from(Buffer.from(cleanedKey, "base64")); | ||
// Clear sensitive data from memory | ||
privateKeyBuffer.fill(0); | ||
|
||
return { options, keyArray }; | ||
} | ||
|
||
export async function getRedirect( | ||
ctx: Context<{ Bindings: Bindings; Variables: Variables }>, | ||
connection: Connection, | ||
) { | ||
const { options, keyArray } = getAppleOptions(connection); | ||
|
||
const apple = new Apple( | ||
options.client_id!, | ||
options.team_id!, | ||
options.kid!, | ||
keyArray, | ||
`${ctx.env.ISSUER}callback`, | ||
); | ||
|
||
const code = nanoid(); | ||
|
||
const authorizatioUrl = await apple.createAuthorizationURL( | ||
code, | ||
options.scope?.split(" ") || ["name", "email"], | ||
); | ||
|
||
const scopes = options.scope?.split(" ") || ["name", "email"]; | ||
if (scopes.some((scope) => ["email", "name"].includes(scope))) { | ||
authorizatioUrl.searchParams.set("response_mode", "form_post"); | ||
} | ||
|
||
return { | ||
redirectUrl: authorizatioUrl.href, | ||
code, | ||
}; | ||
} | ||
|
||
export async function validateAuthorizationCodeAndGetUser( | ||
ctx: Context<{ Bindings: Bindings; Variables: Variables }>, | ||
connection: Connection, | ||
code: string, | ||
) { | ||
const { options, keyArray } = getAppleOptions(connection); | ||
|
||
const apple = new Apple( | ||
options.client_id!, | ||
options.team_id!, | ||
options.kid!, | ||
keyArray, | ||
`${ctx.env.ISSUER}callback`, | ||
); | ||
|
||
const tokens = await apple.validateAuthorizationCode(code); | ||
const idToken = parseJWT(tokens.idToken()); | ||
|
||
if (!idToken) { | ||
throw new Error("Invalid ID token"); | ||
} | ||
|
||
const payload = idTokenSchema.parse(idToken.payload); | ||
|
||
return { | ||
sub: payload.sub, | ||
email: payload.email, | ||
given_name: payload.given_name, | ||
family_name: payload.family_name, | ||
name: payload.name, | ||
picture: payload.picture, | ||
locale: payload.locale, | ||
}; | ||
} |
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,73 @@ | ||
import { Facebook } from "arctic"; | ||
import { Context } from "hono"; | ||
import { Connection } from "@authhero/adapter-interfaces"; | ||
import { nanoid } from "nanoid"; | ||
import { Bindings, Variables } from "../types"; | ||
import { parseJWT } from "oslo/jwt"; | ||
import { idTokenSchema } from "../types/IdToken"; | ||
|
||
export async function getRedirect( | ||
ctx: Context<{ Bindings: Bindings; Variables: Variables }>, | ||
connection: Connection, | ||
) { | ||
const { options } = connection; | ||
|
||
if (!options?.client_id || !options.client_secret) { | ||
throw new Error("Missing required authentication parameters"); | ||
} | ||
|
||
const facebook = new Facebook( | ||
options.client_id, | ||
options.client_secret, | ||
`${ctx.env.ISSUER}callback`, | ||
); | ||
|
||
const code = nanoid(); | ||
|
||
const authorizationUrl = facebook.createAuthorizationURL( | ||
code, | ||
options.scope?.split(" ") || ["email"], | ||
); | ||
|
||
return { | ||
redirectUrl: authorizationUrl.href, | ||
code, | ||
}; | ||
} | ||
|
||
export async function validateAuthorizationCodeAndGetUser( | ||
ctx: Context<{ Bindings: Bindings; Variables: Variables }>, | ||
connection: Connection, | ||
code: string, | ||
) { | ||
const { options } = connection; | ||
|
||
if (!options?.client_id || !options.client_secret) { | ||
throw new Error("Missing required authentication parameters"); | ||
} | ||
|
||
const facebook = new Facebook( | ||
options.client_id, | ||
options.client_secret, | ||
`${ctx.env.ISSUER}callback`, | ||
); | ||
|
||
const tokens = await facebook.validateAuthorizationCode(code); | ||
const idToken = parseJWT(tokens.idToken()); | ||
|
||
if (!idToken) { | ||
throw new Error("Invalid ID token"); | ||
} | ||
|
||
const payload = idTokenSchema.parse(idToken.payload); | ||
|
||
return { | ||
sub: payload.sub, | ||
email: payload.email, | ||
given_name: payload.given_name, | ||
family_name: payload.family_name, | ||
name: payload.name, | ||
picture: payload.picture, | ||
locale: payload.locale, | ||
}; | ||
} |
Oops, something went wrong.