Dev Learning Lab

The labs on this site need JavaScript to run. The same content is written out below.

Learn › Security › Authentication

OAuth login flow

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

Concepts in this lab

OAuth 2.0
An authorization framework that lets a user allow a third-party application limited access to the user's resources without giving it the password. The user logs in at the authorization server (such as Google or Kakao) and gives consent. As a result, the application receives an access token and uses it to access the resources. Social login uses this flow.
Authentication and authorization
Authentication checks who a user is. Authorization decides whether that verified user has permission to use a specific resource or feature. Authentication happens first, and authorization is decided based on its result. In HTTP status codes, a failed authentication corresponds to 401 and a failed authorization corresponds to 403.
Access token and refresh token
Why it exists — When one token keeps a login going, a long lifetime means a stolen token stays usable for a long time, and a short lifetime means the user must log in again often. If you split it into an access token and a refresh token, the token that travels with every API request has a short life, and the login continues through the refresh token.
Where it's used today — It is commonly used in mobile apps and SPAs that keep a login with a JWT. The refresh token is often kept in an HttpOnly cookie or in the app's secure storage, and the server also records it and revokes it when the user logs out.
An access token is the token that a client presents when it calls an API. Its lifetime is kept short to reduce the damage if it is stolen. A refresh token is the token used to get a new access token when the access token expires. It has a long lifetime and is stored more securely. The server can revoke a refresh token to stop new access tokens from being issued.
Cross-site request forgery (CSRF)
An attack that takes advantage of the fact that a user is logged in: a page made by the attacker causes the user's browser to send an unintended request to the target site. The browser attaches cookies automatically, so the server treats it as a normal request. The primary defense is to require requests to include an unpredictable token issued by the server (a CSRF token, which Spring Security checks by default), and the SameSite attribute of cookies is a secondary defense used together with it.

Try these

Running with the default settings

  1. The user presses the "Log in with Kakao" button on our service (shop.example).
  2. Our service redirects the browser to the authorization endpoint of Kakao. It attaches who is making the request (client_id), the address to return to when finished (redirect_uri), the range of information it wants (scope), and a random value that identifies this request (state). Our service remembers the state in the session. Spring Security 7 also attaches a code_challenge. It is the hash of a random value that our server created (the code_verifier), and the original value is kept only in the session. This procedure is called PKCE.
  3. The user logs in on the Kakao page and gives consent to share information. The ID and password are entered only at Kakao. Our service cannot see them.
  4. Kakao redirects the browser back to the redirect_uri of our service and attaches the authorization code. This code is a very short-lived, single-use code that can be exchanged for a token. It passes through the browser address bar, so it is designed on the assumption that it can be exposed.
  5. Our server compares the returned state with the value it remembered in the session. They are the same, so it continues.
  6. Our server sends a request directly to the token endpoint of Kakao. Along with the authorization code, it sends the client_secret, which only our server knows, and the code_verifier, of which only the hash was sent at the start. This communication does not go through the browser.
  7. Kakao checks the code, the secret, and the code_verifier and issues an access token. The token is not exposed to the browser and is delivered only to our server. The authorization code is discarded at this moment.
  8. Our server calls the user information API of Kakao with the access token. It can receive only the information in the scope that the token allows.
  9. The user information has been received. Our service uses the Kakao member number to find our member or to register a new one.
  10. Finally, our service does its own login processing. Later requests are authenticated with the session or token of our service, independent of Kakao.

Result of this run

Code

# 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

Assumptions of this model

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

Concepts in this lab

What this lab shows

Settings

Code


      
    

Diagram

Run comparison

Try these