Skip to content
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 workos authkit provider #12505

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions packages/core/src/providers/authkit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* @module providers/authkit
*/
import { JsonObject } from "oauth4webapi"
import type { OAuthConfig, OAuthUserConfig } from "./index.js"
/**
* - {@link https://api.workos.com/user_management/users/<user_id> | The returned profile object}
*/
export interface AuthKitProfile extends Record<string, any> {
object: string
id: string
organization_id: string
connection_id: string
connection_type: string
idp_id: string
email: string
first_name: string
last_name: string
raw_attributes: {
id: string
email: string
lastName: string
firstName: string
picture: string
}
}

export default function AuthKit<P extends AuthKitProfile>(
options: OAuthUserConfig<P>
): OAuthConfig<P> {
return {
id: "authkit",
name: "AuthKit",
type: "oauth",
authorization: {
url: "https://api.workos.com/user_management/authorize",
params: {
provider: "authkit",
screen_hint: "sign-in",
},
},
token: {
url: "https://api.workos.com/user_management/authenticate",
async conform(res: Response) {
const data = await res.json()
if (data.token_type === "bearer") {
console.warn(
"token_type is 'bearer'. Redundant workaround, please open an issue."
)
return res
}
return Response.json({ ...data, token_type: "bearer" }, res)
},
},
client: {
token_endpoint_auth_method: "client_secret_post",
},
userinfo: {
url: "https://api.workos.com/user_management/users",
async request({ tokens, provider }) {
Copy link
Author

@fierysolid fierysolid Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The /user_management/authenticate (token) call actually returns the user profile at the same time, so you could in theory just immediately return tokens.user, but I assumed there may be other places where the "userinfo" endpoint gets called to get an updated profile.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great reasoning. I think it’s best to wait for an official response from a maintainer regarding how the userinfo property/request works.

const response = await fetch(
`${provider.userinfo?.url}/${(tokens.user as JsonObject)?.id}`,
{
headers: {
Authorization: `Bearer ${provider.clientSecret}`,
},
}
)
return response.json()
},
},
profile(profile) {
return {
id: profile.id,
name: `${profile.first_name} ${profile.last_name}`,
email: profile.email,
image: profile.profile_picture_url ?? null,
}
},
style: { bg: "#6363f1", text: "#fff" },
options,
}
}
Loading