OAuth2 & OpenID Connect Deep Dive · Ders

Genel İstemciler için PKCE

Proof Key for Code Exchange (PKCE) mekanizmasını ve mobil uygulamalar gibi genel istemcileri yetkilendirme kodunun ele geçirilmesi saldırılarından nasıl koruduğunu keşfedin.

1. ders / 412 adım

Genel İstemciler için PKCE, CoddyKit'te ücretsiz bir OAuth2 & OpenID Connect Deep Dive dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, OAuth2 & OpenID Connect Deep Dive öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. OAuth2 & OpenID Connect Deep Dive kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

Public Clients & No Secrets

Imagine a mobile app or a Single-Page Application (SPA) running in a browser. These are known as public clients in OAuth2.

  • They run on devices or environments that can't reliably keep a secret.
  • Unlike a server-side application, they can't securely store a client secret.

This lack of a secret creates a security challenge, making them vulnerable to certain attacks.

Authorization Code Interception

Without a client secret, public clients face a specific risk: the Authorization Code Interception Attack.

  • An attacker might intercept the authorization code sent back to your app.
  • If they get the code, and there's no client secret to verify, they could exchange it for an access token.

This means an attacker could gain access to a user's resources, impersonating your application.

PKCE: Protecting Public Clients

To protect public clients from interception attacks, OAuth2 introduced Proof Key for Code Exchange (PKCE), pronounced "pixy."

  • PKCE adds a dynamic secret to the authorization code flow.
  • This secret is created by the client for each authorization request.

Even if an attacker intercepts the authorization code, they won't have this secret, preventing them from exchanging the code for tokens.

Code Verifier: The Client's Secret

At the heart of PKCE is the code_verifier. It's a cryptographically random string generated by the client application for each authorization attempt.

  • It's a high-entropy secret, meaning it's long and hard to guess.
  • The client keeps this code_verifier private and never sends it directly to the authorization endpoint.

Think of it as a one-time password your app generates and remembers.

Code Challenge: The Public Proof

Instead of sending the code_verifier, the client sends a code_challenge to the authorization server.

  • The code_challenge is a transformed version of the code_verifier.
  • The transformation method (e.g., SHA256 hash then Base64Url encode) is specified by code_challenge_method.

This allows the authorization server to verify the client later without ever knowing the actual code_verifier upfront.

PKCE Flow: Auth Request

Let's trace the PKCE flow. First, the public client (your app) prepares for authorization:

  1. It generates a unique code_verifier.
  2. It transforms this into a code_challenge using S256 (SHA256 hash + Base64Url encoding).
  3. It then sends an authorization request to the Authorization Server, including the code_challenge and code_challenge_method.

Example parameters: code_challenge=xyz&code_challenge_method=S256

PKCE Flow: Auth Code Grant

Upon receiving the authorization request with the code_challenge:

  1. The Authorization Server stores the received code_challenge and its method.
  2. It authenticates the user and obtains their consent.
  3. It then redirects the user back to the client's registered redirect URI, providing an authorization code.

At this point, the client still holds its code_verifier locally.

PKCE Flow: Token Request

Now, with the authorization code in hand, the client needs to exchange it for an access token:

  1. The client makes a POST request to the Authorization Server's token endpoint.
  2. This request includes the authorization code AND the original code_verifier it generated earlier.

This is where the magic happens! The code_verifier acts as proof that this client is the legitimate one.

PKCE Flow: Verification & Tokens

When the Authorization Server receives the token request with the code_verifier:

  1. It recalculates the code_challenge using the provided code_verifier and the stored code_challenge_method.
  2. It compares this newly calculated challenge with the code_challenge it stored in Step 1.
  3. If they match, the client is verified, and the Authorization Server issues access and refresh tokens. Otherwise, the request is denied.

Generating Verifier & Challenge

Here's a simple Java example demonstrating how to generate a code_verifier and its corresponding code_challenge using the S256 method. This is a core part of PKCE implementation.

Try running the code to see the generated values!

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

public class Main {

    public static void main(String[] args) throws NoSuchAlgorithmException {
        // 1. Generate a secure random code_verifier
        SecureRandom sr = new SecureRandom();
        byte[] codeVerifierBytes = new byte[32]; // 32 bytes = 256 bits
        sr.nextBytes(codeVerifierBytes);
        String codeVerifier = Base64.getUrlEncoder().withoutPadding().encodeToString(codeVerifierBytes);

        // 2. Derive the code_challenge using S256 (SHA256 + Base64Url-encode)
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] digest = md.digest(codeVerifier.getBytes(java.nio.charset.StandardCharsets.US_ASCII));
        String codeChallenge = Base64.getUrlEncoder().withoutPadding().encodeToString(digest);

        System.out.println("Code Verifier: " + codeVerifier);
        System.out.println("Code Challenge: " + codeChallenge);
        System.out.println("Method: S256");
    }
}

PKCE Quick Check

PKCE adds a vital layer of security for public clients. Which of the following best describes the primary problem PKCE solves?

Recap: PKCE's Security Layer

We've learned about PKCE, a crucial security extension for OAuth2, especially for public clients like mobile apps and SPAs.

  • Public clients can't securely store client secrets.
  • PKCE uses a one-time code_verifier and its transformed code_challenge to verify the legitimate client.
  • This protects against Authorization Code Interception attacks, ensuring only the intended client can exchange the authorization code for tokens.

PKCE makes OAuth2 flows much more secure for applications that operate in less trusted environments.

Başlamak ücretsiz

Yapay zeka eğitmeniyle OAuth2 & OpenID Connect Deep Dive öğren — ücretsiz

Tarayıcında gerçek kod yaz ve çalıştır, 7/24 yapay zeka eğitmeninden anında yardım al; web'de ya da uygulamada kaldığın yerden devam et.

Kurslar
12
Dersler
48

Sıkça Sorulan Sorular

“Genel İstemciler için PKCE” dersi ücretsiz mi?

Evet — “Genel İstemciler için PKCE” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve OAuth2 & OpenID Connect Deep Dive kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. OAuth2 & OpenID Connect Deep Dive kursu toplamda 4 dersten oluşur.

“Genel İstemciler için PKCE” dersinde ne öğreneceğim?

Proof Key for Code Exchange (PKCE) mekanizmasını ve mobil uygulamalar gibi genel istemcileri yetkilendirme kodunun ele geçirilmesi saldırılarından nasıl koruduğunu keşfedin. OAuth2 & OpenID Connect Deep Dive ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

OAuth2 & OpenID Connect Deep Dive öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te OAuth2 & OpenID Connect Deep Dive, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.

“Genel İstemciler için PKCE” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu OAuth2 & OpenID Connect Deep Dive dersinde kod yazıp çalıştırabilir miyim?

Evet. Her OAuth2 & OpenID Connect Deep Dive dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Genel İstemciler için PKCE
  2. Yenileme Belirteçleri ve Kapsamlar
  3. Kaynak Sahibi Parola Kimlik Bilgileri
  4. Belirteç Değişimi (RFC 8693)
← OAuth2 & OpenID Connect Deep Dive Sayfasına Dön