Security
Social login
Sign in with Google, GitHub, Microsoft, Facebook, X and Apple, and any OpenID Connect provider.
OAuth2Provider runs the OAuth 2.0 authorization-code flow for you. You create one provider per service, send the user to it with begin, and call complete on the callback route to get an OAuthProfile. Then you decide what that means in your application: find or create the user, and issue your own token or session.
One provider, one controller
@Injectable
class SocialProviders(config: ConfigService) {
val google = OAuth2Provider.google {
clientId = config.getOrThrow("GOOGLE_CLIENT_ID"); clientSecret = config.getOrThrow("GOOGLE_CLIENT_SECRET")
redirectUri = "https://app.example.com/auth/google/callback"; stateSecret = config.getOrThrow("OAUTH_STATE_SECRET")
}
}
@Controller("/auth/google")
class GoogleLogin(private val providers: SocialProviders, private val users: UserService, private val jwt: JwtService) {
@Public @Get fun login(@Response res: RawResponse) { providers.google.begin(res) } // redirects to Google
@Public @Get("/callback")
fun callback(ctx: HttpContext): TokenDto {
val profile = providers.google.complete(ctx) // checks state and PKCE, exchanges the code, loads the profile
val user = users.findOrCreate(profile.provider, profile.id, profile.email, profile.name)
return TokenDto(jwt.sign(mapOf("sub" to user.id)))
}
}OAuthProfile has provider, id (the provider's stable user id: store this, not the email), email, name, avatarUrl, accessToken, refreshToken and raw, the provider's whole answer. Each provider needs an application registered in its developer console, which gives you the client id and secret and lets you register the redirectUri.
The providers
OAuth2Provider.google { clientId = ...; clientSecret = ...; redirectUri = ".../auth/google/callback"; stateSecret = ... }Scopes openid email profile. Add authorizeParams = mapOf("access_type" to "offline") to receive a refresh token.
GitHub
OAuth2Provider.github { clientId = ...; clientSecret = ...; redirectUri = ".../auth/github/callback"; stateSecret = ... }Scopes read:user user:email. A private email is fetched from GitHub's /user/emails (the primary, verified one).
Microsoft (Entra ID, personal and work accounts)
OAuth2Provider.microsoft(tenant = "common") { clientId = ...; clientSecret = ...; redirectUri = ".../auth/microsoft/callback"; stateSecret = ... }
// tenant: "common" (any account), "organizations", "consumers", or your tenant id / domain for a single organisationScopes openid email profile. The id is the account's sub.
Facebook / Meta
OAuth2Provider.facebook { clientId = appId; clientSecret = appSecret; redirectUri = ".../auth/facebook/callback"; stateSecret = ... }Scopes email public_profile. Facebook does not accept PKCE, so the preset leaves it out and relies on the signed state; the token is sent to the Graph API as access_token. The email is there only if the user has a confirmed one and grants it.
X (Twitter)
OAuth2Provider.x { clientId = ...; clientSecret = ...; redirectUri = ".../auth/x/callback"; stateSecret = ... }OAuth 2.0 with PKCE, scopes tweet.read users.read offline.access, client authentication by HTTP Basic (as X requires). X does not share an email address through this API, so profile.email is null: identify the user by profile.id and ask for an email yourself.
Apple
OAuth2Provider.apple(teamId = "ABCDE12345", keyId = "KEY1234567", privateKeyPem = File("AuthKey_KEY1234567.p8").readText()) {
clientId = "com.example.web" // your Services ID
redirectUri = "https://app.example.com/auth/apple/callback"
stateSecret = ...
}
@Controller("/auth/apple")
class AppleLogin(private val apple: SocialProviders, ...) {
@Public @Get fun login(@Response res: RawResponse) { apple.apple.begin(res) }
@Public @Post("/callback") fun callback(ctx: HttpContext): TokenDto { val profile = apple.apple.complete(ctx); ... } // a POST!
}Apple differs from the others in four ways, all handled by the preset:
- The client secret is a JWT signed with ES256 by the
.p8key you download; it is generated for each exchange. - The callback is a POST (
response_mode=form_post), so the route is@Post. Exclude it from CSRF protection (useCsrf { ignorePaths = listOf("/auth/apple/callback") }): the request comes from Apple's page, not yours. - The state cookie is
SameSite=None; Secure, because the POST arrives from another site; this needs HTTPS. - The profile comes from the
id_token(Apple has no user-info endpoint); its audience, expiry and a nonce are checked. The user's name is sent only the first time they authorise your app, so save it then.
Apple also hides the email behind a relay address if the user asks (...@privaterelay.appleid.com).
Any other OpenID Connect or OAuth 2 provider
OAuth2Provider.custom("keycloak") {
clientId = ...; clientSecret = ...; redirectUri = ...; stateSecret = ...
authorizeUrl = "https://sso.example.com/realms/app/protocol/openid-connect/auth"
tokenUrl = "https://sso.example.com/realms/app/protocol/openid-connect/token"
userInfoUrl = "https://sso.example.com/realms/app/protocol/openid-connect/userinfo"
scopes = listOf("openid", "email", "profile")
}Provider quirks have switches: usePkce, clientAuthBasic, userInfoTokenParam, cookieSameSite, responseMode, useNonce, profileFromIdToken, and mapProfile to shape the profile.
How it stays safe
The state and the PKCE verifier travel in a signed, ten-minute, single-use cookie, so no server-side state is needed and several instances work together (give them all the same stateSecret). A forged or missing state, a tampered cookie, a refused code and an error= answer are all 401s.
What is tested
The flow is tested end to end against local identity providers written for the tests, shaped like Google-style OIDC, X (Basic authentication, nested profile), Facebook (no PKCE, token in the query, nested picture) and Apple (ES256 client secret verified with a real key pair, cross-site POST callback, id_token audience and nonce checks). The presets use each provider's documented endpoints but were not run against the live services, which needs your credentials: do a real sign-in with each before launch.