ความปลอดภัยของโทเค็น (เข้าถึงและต่ออายุ)
ศึกษาแนวทางปฏิบัติที่ดีที่สุดสำหรับการจัดเก็บ ส่งผ่าน และทำให้โทเค็นเข้าถึงกับโทเค็นต่ออายุหมดอายุอย่างปลอดภัย
ความปลอดภัยของโทเค็น (เข้าถึงและต่ออายุ) เป็นบทเรียน OAuth2 & OpenID Connect Deep Dive ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน OAuth2 & OpenID Connect Deep Dive และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Token Security Matters
Welcome to Token Security! In this lesson, we'll dive into protecting the vital components of OAuth2 and OpenID Connect: access tokens and refresh tokens.
These tokens are like digital keys. If they fall into the wrong hands, unauthorized access to your users' data or your application's resources can occur. Securing them is paramount.
Understanding Access Tokens
Access tokens are credentials that grant a client application permission to access specific resources on behalf of the user. Think of them as a temporary pass.
- They have a short lifespan (minutes to hours).
- They are used directly to authorize API requests.
- If compromised, the damage is limited due to their short expiry.
Understanding Refresh Tokens
Refresh tokens are used to obtain new access tokens after the current one expires, without requiring the user to re-authenticate. They are like a master key.
- They have a much longer lifespan (days, weeks, or even months).
- They are highly sensitive because their compromise can grant continuous access.
- They should be stored with the highest level of security.
Secure Transmission: HTTPS/TLS
The most fundamental rule for token security is to always transmit tokens over HTTPS (TLS). This encrypts the communication channel between the client and the server.
Without HTTPS, tokens sent over an unsecured network (like public Wi-Fi) could be easily intercepted by attackers, leading to immediate compromise.
Storing Access Tokens Safely
Access tokens, due to their short lifespan, require careful client-side storage:
- Web Applications: Store in memory (JavaScript variables) or secure, httpOnly cookies. Avoid
localStorageorsessionStoragedue to XSS vulnerability. - Mobile Applications: Use platform-specific secure storage like iOS Keychain or Android Keystore.
Simulating Token Expiry
Access tokens are designed to expire. This code snippet shows a conceptual way to check if a token, represented by its issue and expiry times, is still valid.
public class Main {
public static void main(String[] args) {
long issuedAtMillis = System.currentTimeMillis() - (5 * 60 * 1000); // Token issued 5 minutes ago
long expiresInMillis = 10 * 60 * 1000; // Token expires in 10 minutes from issue
long expiresAtMillis = issuedAtMillis + expiresInMillis;
System.out.println("Token issued 5 minutes ago.");
System.out.println("Expires 10 minutes from issue.");
if (System.currentTimeMillis() > expiresAtMillis) {
System.out.println("Status: Token EXPIRED.");
} else {
System.out.println("Status: Token ACTIVE.");
}
}
}Securing Refresh Tokens
Refresh tokens are the most critical to protect due to their long lifespan. Their storage must be even more stringent:
- Web Applications: Exclusively use httpOnly, secure cookies. Never store in JavaScript-accessible storage.
- Mobile Applications: Utilize hardware-backed secure storage (e.g., Secure Enclave on iOS, TEE on Android) if available.
Refresh Token Rotation (RTR)
Refresh Token Rotation (RTR) is a crucial security enhancement. Each time a client uses a refresh token to get a new access token, the authorization server should issue a new refresh token and invalidate the old one.
If an old refresh token is stolen and used, the legitimate client will detect it because its current refresh token will no longer work, signaling a potential breach.
Token Revocation Strategies
Tokens should not just expire; they should also be revocable. The authorization server must provide mechanisms to invalidate tokens prematurely.
- User Logout: All associated access and refresh tokens should be revoked.
- Password Change: Revoke all active tokens to force re-authentication.
- Suspicious Activity: If a token is suspected of being compromised, it should be immediately revoked.
Managing Token Lifespans
Balancing security and user experience is key:
- Access Tokens: Keep their lifespan short (e.g., 5-15 minutes). This limits the window of opportunity for attackers if a token is compromised.
- Refresh Tokens: Can have longer lifespans (e.g., 7-30 days), but must be revocable and ideally protected with RTR.
Token Security Check
Which of the following are recommended best practices for securing OAuth2/OIDC tokens?
Recap: Secure Tokens
You've learned the critical practices for securing access and refresh tokens!
- HTTPS/TLS is non-negotiable for transmission.
- Access tokens need secure, short-lived storage (memory, httpOnly cookies).
- Refresh tokens demand the highest security (httpOnly, secure cookies, hardware-backed storage) and should use Refresh Token Rotation.
- Implement effective revocation strategies and manage token lifespans wisely.
คำถามที่พบบ่อย
บทเรียน “ความปลอดภัยของโทเค็น (เข้าถึงและต่ออายุ)” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ความปลอดภัยของโทเค็น (เข้าถึงและต่ออายุ)” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส OAuth2 & OpenID Connect Deep Dive ให้อัปเกรดเป็น CoddyKit PRO คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ความปลอดภัยของโทเค็น (เข้าถึงและต่ออายุ)”
ศึกษาแนวทางปฏิบัติที่ดีที่สุดสำหรับการจัดเก็บ ส่งผ่าน และทำให้โทเค็นเข้าถึงกับโทเค็นต่ออายุหมดอายุอย่างปลอดภัย คุณปฏิบัติ OAuth2 & OpenID Connect Deep Dive ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน OAuth2 & OpenID Connect Deep Dive หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน OAuth2 & OpenID Connect Deep Dive บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “ความปลอดภัยของโทเค็น (เข้าถึงและต่ออายุ)” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน OAuth2 & OpenID Connect Deep Dive นี้ได้ไหม
ได้ บทเรียน OAuth2 & OpenID Connect Deep Dive ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ความปลอดภัยของโทเค็น (เข้าถึงและต่ออายุ)
- พารามิเตอร์ state และ CSRF
- แนวทางปฏิบัติที่ดีสำหรับประเภทการมอบสิทธิ์
- การรักษาความปลอดภัย URI เปลี่ยนเส้นทาง