OAuth vs. ODIC—which one should I choose...? ODIC ...
# ktor
m
OAuth vs. ODIC—which one should I choose...? ODIC doesn’t have a wiki, but... I figured it should be okay to use anyway, so I’m posting this since I couldn’t find a solution. I’m using Google, and I’m debating whether to include a JWT—to be honest, this is my first time handling login... Are there any good examples out there?
a
If your goal is to implement Sign in with Google, I'd recommend OIDC. OAuth 2.0 is primarily about delegated authorization, while OIDC builds on top of it to provide authentication. Regarding JWTs, Google already issues an ID token (which is a JWT) through OIDC. You should validate that token and use its "sub" claim to identify the user. You don't necessarily need to issue your own JWT unless your application's architecture requires it. For a typical web application, a secure session cookie is often sufficient but i prefer and recommend to use JWT Also, Ktor 3.6 introduced an experimental OIDC plugin for the JVM, and there's now an official browser-login example you can follow: https://ktor.io/docs/server-oidc-browser-login.html I'd recommend starting with that example rather than implementing the authentication flow from scratch.
❤️ 2
➕ 5
m
thx
😄
a
??
m
what..?
Is the implementation wrong…?
a
I mean why did you share the code?
Let me check it
m
It’s just that the method I implemented might be helpful to others... That’s why I’m asking, and I’d also like to get some feedback.
👍🏼 1
a
Overall is good but i noticed a couple of minor issues.
m
Oh, could you tell me what it is??
a
Yes, wait a minute.
👀 1
A few things I’d fix 1. Catch unexpected exceptions in
onAuthenticated
.
Only
AuthException
is caught, so a DB failure in
loginWithOAuth
would give the user a 500 mid-redirect. A fallback
catch (e: Exception)
that logs at
error
and redirects to
/login?error=server_error
would fix that (rethrow
CancellationException
first). Also,
respondRedirect(frontendUrl)
currently sits inside the
try
, so I’d move it after the
try
to avoid a double redirect if it fails. 2. Email verification.
emailVerified
is passed to the service, but I can’t see where it’s enforced. If
loginWithOAuth
links accounts by email, it should reject
emailVerified == false
to avoid account takeover. Could you confirm where that’s handled? 3. Log the cause properly.
"...failed: $cause"
loses the stack trace. If
cause
is a
Throwable
, use
log.warn("...", cause)
. Users cancelling the consent screen will also hit this path, so
info
may be better for that case. Minor / non-blocking: •
suspend
on
configureSecurity
may be unnecessary. Try dropping it and see if it compiles. • Validate
frontendUrl
at startup (e.g. no trailing slash), since
"$frontendUrl/login"
breaks quietly otherwise. • Pull the error codes (
login_failed
,
auth_failed
) into constants shared with the frontend contract. • Consider extracting the
UserInfo → OAuthIdentity
mapping into a small function so it’s unit-testable.
m
ohh..
thx
a
kodee loving
❤️ 1
m
However,
suspend
is absolutely necessary because oidc.identityProvider only runs in a non-blocking context.
kodee intensifies
a
If
suspend fun
is necessary keep it.
One more issue: According to Ktor’s official documentation, the OIDC session is stored before
onAuthenticated
is called. If
loginWithOAuth()
rejects the user, the OIDC session may still remain active. I’d recommend clearing it in both exception handlers using
call.sessions.clear<OidcToken.Id>()
m
what??
Yeah, I have a question now. After a user logs in, how can I retrieve their role??? I want to create an admin-only page, but to do that, I have to put all the routing in
auth
...
only
authenticateWith(google.session)
in
call.principal.userInfo.subject
..? Isn’t there another way?
a
With Ktor, there’s always another way.
google.session
exposes the OIDC principal, which gives you Google’s user information, but not necessarily your application’s roles. I’d recommend using
google.session.mapPrincipal()
to resolve the Google subject to your application’s user from the database, including their role. Then you can use
authenticateWith()
for protected routes and
withRoles()
for admin-only routes. You don’t need to put everything inside `configureSecurity()`; you can reuse the authentication scheme across your routing modules. Official docs: ktor.io/docs/server-typed-auth.html
m
ohh hard.. sorry can u send a example..?
I just switched over from Spring... Ktor seems really hard... I switched because I didn’t like annotations, but Ktor is complicated... If you get the structure wrong even once, everything gets tangled up... What’s the best way to learn this?
a
I’m a Spring Boot developer myself. You just have to change the way you think when switching from Spring to Ktor.
m
I'll try it at school.