PKCE e proteção de clientes públicos
Aprenda como a extensão PKCE protege o fluxo OAuth2 de código de autorização em aplicativos móveis e de página única que não podem manter um segredo de cliente.
PKCE e proteção de clientes públicos é uma aula grátis de Spring Security 6 & JWT Authentication no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Spring Security 6 & JWT Authentication, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Spring Security 6 & JWT Authentication inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
The Public Client Problem
Mobile apps and SPAs are public clients: their code ships to the user, so they cannot safely store a client secret. Without a secret, the plain Authorization Code flow is vulnerable to interception.
What PKCE Solves
PKCE (Proof Key for Code Exchange, pronounced 'pixy') adds a dynamic secret per authorization request. Even if the authorization code is stolen, it cannot be exchanged without the matching proof.
The Code Verifier
The client generates a random, high-entropy string called the code_verifier and keeps it in memory for this one flow.
const codeVerifier = base64url(randomBytes(32));The Code Challenge
The client hashes the verifier with SHA-256 to make the code_challenge. The hash is sent to the server, but the original verifier never leaves the device yet.
const codeChallenge = base64url(sha256(codeVerifier));Starting the Authorization Request
The client sends the challenge and the method (S256) to the authorization endpoint along with the usual parameters.
GET /authorize?response_type=code
&client_id=app123
&code_challenge=XYZ...
&code_challenge_method=S256Server Stores the Challenge
The authorization server remembers the code_challenge and links it to the authorization code it issues after the user logs in.
Exchanging the Code
When swapping the code for tokens, the client now reveals the original code_verifier.
POST /token
grant_type=authorization_code
&code=abc123
&code_verifier=ORIGINAL_RANDOMServer Verifies the Proof
The server hashes the received verifier and compares it to the stored challenge. If they match, the requester is the same party that started the flow; otherwise it rejects the exchange.
if (sha256(received_verifier) !== stored_challenge) {
reject('invalid_grant');
}Why It Stops Interception
An attacker who steals the authorization code (for example via a malicious app on the device) still cannot use it: they never saw the code_verifier, which existed only inside the legitimate client's memory.
PKCE Is Now Recommended for All
Originally for mobile, PKCE is now recommended for every Authorization Code flow, including confidential web clients. OAuth 2.1 makes it the default.
Library Support
You rarely implement PKCE by hand. Libraries like AppAuth, oidc-client-ts, and Spring Authorization Server handle verifier generation, hashing, and validation for you.
Quick Check
Test your understanding of PKCE.
Recap
You learned how PKCE secures public clients:
- Public clients cannot keep a secret, so the plain code flow is unsafe
- The client creates a
code_verifierand sends its hash as thecode_challenge - The verifier is revealed only at token exchange
- A stolen code is useless without the verifier
PKCE is now the default for all Authorization Code flows.
Perguntas Frequentes
A aula “PKCE e proteção de clientes públicos” é grátis?
Sim — o texto completo de “PKCE e proteção de clientes públicos” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Spring Security 6 & JWT Authentication, atualize para CoddyKit PRO. O curso de Spring Security 6 & JWT Authentication inclui 4 aulas no total.
O que vou aprender em “PKCE e proteção de clientes públicos”?
Aprenda como a extensão PKCE protege o fluxo OAuth2 de código de autorização em aplicativos móveis e de página única que não podem manter um segredo de cliente. Você pratica Spring Security 6 & JWT Authentication com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Spring Security 6 & JWT Authentication?
Nenhuma experiência prévia é necessária. Spring Security 6 & JWT Authentication no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “PKCE e proteção de clientes públicos”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Spring Security 6 & JWT Authentication?
Sim. Cada aula de Spring Security 6 & JWT Authentication inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Visão geral do protocolo OAuth2
- Introdução ao OpenID Connect
- Tipos comuns de concessão do OAuth2
- PKCE e proteção de clientes públicos