The labs on this site need JavaScript to run. The same content is written out below.
Learn › Security › Authentication
What is exchanged when you click "Log in with Kakao"
Why it exists — If a user is asked for their ID and password of another service in order to fetch data from that service, the receiving side gets every permission, more places can leak the password, and the user cannot cut off that access until they change the password. OAuth 2.0 has a token with a defined scope and expiry issued instead of the password, so that access is limited to what the user allowed and that permission alone can be withdrawn later.
Where it's used today — It is used for social login such as "Log in with Kakao" and "Log in with Google", and for integrations that read and write, on a user's behalf, data in another service, such as their calendar or drive. In Spring, Spring Security's OAuth2 client handles this flow. For login purposes, OpenID Connect, which standardizes identity verification on top of OAuth 2.0, is sometimes used together.
Social login is a procedure in which our service gets permission to access a user's information, with the user's consent, without ever receiving the user's password. Some things travel through the browser, and others are exchanged directly between servers.
Checked against real execution · The four situations were run with the OAuth2 client (the application.yml on the screen), the OAuth 2.0 authorization server, and a resource server of Spring Security 7.1.1 started on 127.0.0.1, and the order, path, and parameters of the requests exchanged and the results were checked. The real Kakao was not contacted, and what happens when the state is not verified follows RFC 6749
# application.yml (Spring Security OAuth2 client)
spring.security.oauth2.client:
registration.kakao:
client-id: abc123 # a value that may be public
client-secret: "********" # a value only the server knows (hidden here)
client-authentication-method: client_secret_post
authorization-grant-type: authorization_code
redirect-uri: https://shop.example/login/oauth2/code/kakao
scope: profile_nickname, account_email
# Kakao is not a provider whose addresses Spring Security already knows,
# so the addresses of its authorization server and API server are written here
provider.kakao:
authorization-uri: https://kauth.kakao.com/oauth/authorize
token-uri: https://kauth.kakao.com/oauth/token
user-info-uri: https://kapi.kakao.com/v2/user/me
user-name-attribute: id
# Spring Security handles the steps below for you
This is the flow of the authorization code grant of OAuth 2.0 (RFC 6749 Section 4.1). Kakao is only an example, and other providers such as Google and Naver have the same flow. The real Kakao was not contacted. All four situations were run with real software started on this computer (127.0.0.1), and it was confirmed that the order and the path of the requests exchanged (through the browser or directly between servers), the names of the parameters they carry, and the results match the screen. Our service is an OAuth2 client of Spring Boot 4.1.1 and Spring Security 7.1.1, started with the application.yml on the screen in which only the addresses and the masked secret were changed. In place of the Kakao authorization server is the OAuth 2.0 authorization server of Spring Security 7.1.1 (spring-security-oauth2-authorization-server), and in place of the Kakao API server is a user information API built as a Spring Security resource server. The test program took the role of the browser by following the redirects one step at a time, and the accounts and secrets are all test values. That the user password is delivered only to the authorization server and that the client_secret is carried only in server-to-server requests was confirmed by recording every request the three servers received. The client of Spring Security 7.1.1 uses PKCE (code_challenge and code_verifier) by default even when it has a client_secret, and the authorization server of the same version rejects an authorization request without a code_challenge. It was also confirmed that when an authorization code is used twice, the second request is rejected with invalid_grant. The lifetime of an authorization code is 5 minutes by default in the Spring authorization server (RFC 6749 Section 4.1.2 recommends a maximum of 10 minutes), and it was confirmed that the code of a client whose lifetime was reduced to 2 seconds is rejected when it is sent 3 seconds later. The shape of error responses and of the user information differs by provider, so the responses of the real Kakao can differ from what was confirmed here. What happens when the state is not verified (the victim ends up logged in to the account of the attacker) was not run and follows the explanation in RFC 6749 Section 10.12. Values such as state=x9f2 and code=Aq7Lp are short fake values written for explanation, and the real values are long random strings that differ on every run (in the test run the state was 44 characters and the authorization code was 128 characters).
Previous lab: JWT structure and verification · Next lab: Session hijacking and session fixation
Struck-through text was removed from the first version of the code; highlighted text was added.