การมอบสิทธิ์อุปกรณ์
เรียนรู้การมอบสิทธิ์อุปกรณ์ของ OAuth2 (RFC 8628) ซึ่งใช้กับอุปกรณ์ที่มีข้อจำกัดด้านการป้อนข้อมูล เช่น สมาร์ททีวี เครื่องเล่นเกม และเครื่องมือ CLI เพื่อรับโทเค็นผ่านอุปกรณ์รอง
การมอบสิทธิ์อุปกรณ์ เป็นบทเรียน OAuth2 & OpenID Connect Deep Dive ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน OAuth2 & OpenID Connect Deep Dive และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why a Device Grant?
Some clients have no browser or only a limited keypad: smart TVs, media consoles, printers, and CLI tools. The classic Authorization Code Flow assumes a rich browser for the user-agent redirect, which these devices cannot provide.
The Device Authorization Grant (RFC 8628) solves this by letting the user complete authorization on a second device (phone or laptop) while the constrained device polls for the result.
The Two Endpoints
The flow introduces a new device authorization endpoint alongside the standard token endpoint.
/device_authorization— the device requests codes here./token— the device polls here with grant typeurn:ietf:params:oauth:grant-type:device_code.
No redirect URI is involved at all.
Step 1: Requesting Device Codes
The device makes a POST to the device authorization endpoint with its client_id and desired scope.
POST /device_authorization HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded
client_id=tv-app-123&scope=profile emailStep 2: The Response
The server returns a device_code (used by the machine), a user_code (typed by the human), a verification_uri, an expires_in, and an interval for polling.
{
"device_code": "GmRhmhcxhwAzkoEqiMEg",
"user_code": "WDJB-MJHT",
"verification_uri": "https://example.com/device",
"expires_in": 900,
"interval": 5
}Step 3: User Instructions
The device displays a short message: Go to example.com/device and enter code WDJB-MJHT.
A verification_uri_complete may also be returned, embedding the code so a QR code can carry the whole link.
Step 4: User Authorizes
On their phone or laptop, the user opens the verification URI, logs in, enters the user_code, and approves the requested scopes. This happens in a full browser, so MFA and rich consent screens all work normally.
Step 5: Device Polls the Token Endpoint
Meanwhile the device polls the token endpoint at the given interval, sending the device_code.
POST /token HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded
grant_type=urn:ietf:params:oauth:grant-type:device_code
&device_code=GmRhmhcxhwAzkoEqiMEg
&client_id=tv-app-123Polling Responses
Until the user finishes, the server returns errors that tell the device how to behave:
authorization_pending— keep polling, user has not approved yet.slow_down— increase the interval by 5 seconds.access_denied— user rejected; stop.expired_token— codes expired; restart.
Step 6: Success
Once the user approves, the next poll returns a normal token response with an access_token (and optionally a refresh_token), exactly like other grants.
{
"access_token": "eyJhbGciOi...",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "8xLOxBtZp8"
}A Simple Poll Loop
A pseudo-implementation of the polling logic, respecting slow_down:
let interval = 5;
while (true) {
await sleep(interval * 1000);
const res = await pollToken(deviceCode);
if (res.access_token) return res;
if (res.error === 'slow_down') interval += 5;
else if (res.error === 'authorization_pending') continue;
else throw new Error(res.error);
}Security Considerations
Keep user_codes short but high-entropy to resist brute force, and rate-limit the verification page. Because there is no redirect, phishing risk shifts to the verification URI — always show the user exactly which app and scopes they are approving.
Quick Check
Test your understanding of the device grant.
Recap
The Device Authorization Grant lets browserless devices authenticate users via a second device.
- Device gets a
device_code+user_codefrom the device endpoint. - User approves on a phone/laptop at the verification URI.
- Device polls the token endpoint, handling
authorization_pendingandslow_down. - On approval it receives normal access and refresh tokens.
คำถามที่พบบ่อย
บทเรียน “การมอบสิทธิ์อุปกรณ์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การมอบสิทธิ์อุปกรณ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส OAuth2 & OpenID Connect Deep Dive ให้อัปเกรดเป็น CoddyKit PRO คอร์ส OAuth2 & OpenID Connect Deep Dive มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การมอบสิทธิ์อุปกรณ์”
เรียนรู้การมอบสิทธิ์อุปกรณ์ของ OAuth2 (RFC 8628) ซึ่งใช้กับอุปกรณ์ที่มีข้อจำกัดด้านการป้อนข้อมูล เช่น สมาร์ททีวี เครื่องเล่นเกม และเครื่องมือ CLI เพื่อรับโทเค็นผ่านอุปกรณ์รอง คุณปฏิบัติ OAuth2 & OpenID Connect Deep Dive ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน OAuth2 & OpenID Connect Deep Dive หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน OAuth2 & OpenID Connect Deep Dive บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การมอบสิทธิ์อุปกรณ์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน OAuth2 & OpenID Connect Deep Dive นี้ได้ไหม
ได้ บทเรียน OAuth2 & OpenID Connect Deep Dive ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- โฟลว์รหัสอนุญาตสิทธิ์
- โฟลว์ข้อมูลรับรองไคลเอ็นต์
- โฟลว์แบบแฝงและการเลิกใช้งาน
- การมอบสิทธิ์อุปกรณ์