మీ సొంత APIలు
ఒక యాప్ మీ APIలలో ఒకదాని కోసం యాక్సెస్ టోకెన్ అడగవచ్చు. దానికి సైన్ చేసిన ఒక JWT వస్తుంది, దాని ఆడియన్స్ ఆ API మాత్రమే, మరేదీ కాదు, మీ API దాన్ని Usherను కాల్ చేయకుండానే మీ ఎన్విరాన్మెంట్ ప్రచురించిన కీలతో తనిఖీ చేస్తుంది. ఒక API కోసం ఇచ్చిన టోకెన్ మరో APIలో పనికిరాదు.
APIకి పేరు పెట్టండి
Usher Consoleలో, ఎన్విరాన్మెంట్ను తెరిచి, ఆపై Applications → Your APIs → Add an API ఎంచుకోండి. దానికి ఒక పేరు, ఒక ఐడెంటిఫైయర్ ఇవ్వండి: https://api.example.com వంటి https:// చిరునామా, లేదా urn:example:api:notes వంటి URN. యాప్ అడిగేది, టోకెన్ యొక్క aud క్లెయిమ్ చెప్పేది ఈ ఐడెంటిఫైయరే (RFC 8707).
తర్వాత దాన్ని కాల్ చేసే యాప్లకు APIని ఇవ్వండి: ప్రతి అప్లికేషన్లోని APIs. యాప్ ఏమి అడిగినా, దానికి ఇచ్చిన APIల కోసం మాత్రమే టోకెన్లు వస్తాయి. ఒక APIని తీసివేస్తే అది ఇక టోకెన్లను ఇవ్వదు; ఇప్పటికే బయట ఉన్నవి కొన్ని నిమిషాల్లో గడువు తీరిపోతాయి.
దాన్ని అడగండి
యాప్ తన ఆథరైజేషన్ అభ్యర్థనకు, టోకెన్ అభ్యర్థనలకు resource=<identifier>ను జోడిస్తుంది. మొబైల్ SDKలు ఇది మీ కోసం చేస్తాయి:
let usher = UsherClient(configuration: .init(
issuer: URL(string: "https://login.example.com")!,
clientID: "…",
redirectURI: URL(string: "com.example.notes:/oauth/callback")!,
resources: ["urn:example:api:notes"]))
var request = URLRequest(url: URL(string: "https://api.example.com/notes")!)
try await usher.authorize(&request) // Authorization: Bearer <a JWT for the Notes API>val usher = Usher.client(context, UsherConfiguration(
issuer = "https://login.example.com",
clientId = "…",
redirectUri = "com.example.notes:/oauth/callback",
resources = listOf("urn:example:api:notes")))మీ APIలలో రెండింటిని కాల్ చేసే యాప్ రెండింటినీ ఒకేసారి అడగవచ్చు; అప్పుడు టోకెన్ ఆడియన్స్ రెండింటినీ జాబితా చేస్తుంది.
మీ APIలో దాన్ని తనిఖీ చేయండి
టోకెన్లోని దేన్నైనా నమ్మే ముందు, ప్రతి అభ్యర్థన టోకెన్ను తనిఖీ చేయండి:
- సంతకం, మీ ఎన్విరాన్మెంట్ డిస్కవరీ డాక్యుమెంట్లోని
jwks_uriవద్ద ఉన్న కీలతో. అల్గారిథమ్ను కీ నుండి తీసుకోండి, టోకెన్ హెడర్ నుండి ఎప్పుడూ కాదు. issసరిగ్గా మీ ఎన్విరాన్మెంట్ ఇష్యూయర్ అయి ఉండాలి. దాన్ని మీ కాన్ఫిగరేషన్లో స్థిరంగా ఉంచండి; టోకెన్ నుండి ఎప్పుడూ చదవకండి.audమీ API ఐడెంటిఫైయర్ను కలిగి ఉండాలి. (openidస్కోప్తో అది మీ ఎన్విరాన్మెంట్ userinfo చిరునామాను కూడా జాబితా చేస్తుంది.)- హెడర్లోని
typవిలువat+jwtఅయి ఉండాలి, తద్వారా ID టోకెన్ను యాక్సెస్ టోకెన్గా చలామణి చేయలేరు. expగడిచిపోయి ఉండకూడదు. యాక్సెస్ టోకెన్లు 10 నిమిషాలు ఉంటాయి; SDKలు వాటిని మీ కోసం రిఫ్రెష్ చేస్తాయి.
Node.jsలో, @usher/node ఇదంతా చేస్తుంది:
import { createVerifier } from '@usher/node'
const usher = await createVerifier({
issuer: 'https://login.example.com',
audience: 'urn:example:api:notes',
})
const claims = await usher.accessToken(bearerToken) // throws if any check failsASP.NET Coreలో:
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(o =>
{
o.Authority = "https://login.example.com"; // discovery and keys come from here
o.MapInboundClaims = false;
o.TokenValidationParameters = new()
{
ValidIssuer = "https://login.example.com",
ValidAudience = "urn:example:api:notes",
ValidTypes = ["at+jwt"],
};
});టోకెన్ ఏమి చెబుతుంది
| క్లెయిమ్ | |
|---|---|
sub | ఆ వ్యక్తి. issతో కలిపి, వారిని గుర్తుంచుకోవడానికి ఉపయోగించే కీ. |
client_id, azp | అడిగిన యాప్. |
scope | యాప్ ఏమి చేయవచ్చని వ్యక్తి అంగీకరించారో అది. |
email, email_verified | email స్కోప్తో: వ్యక్తి చిరునామా, అది వారిదేనని Usher నిరూపించిందా లేదా. |
sid | వారి సైన్-ఇన్ సెషన్. |
org | organization స్కోప్తో: వారి క్రియాశీల సంస్థ, పాత్ర. |
మీకు ఇప్పటికే తెలిసిన వ్యక్తులు
మీ APIలో ఇంతకు ముందు నుండే ఖాతాలు ఉంటే, ఒక వ్యక్తి Usher టోకెన్తో మొదటిసారి వచ్చినప్పుడు వారి ఖాతాను కనుగొనవచ్చు: వారిని email ద్వారా వెతకండి, email_verified విలువ true అయినప్పుడు మాత్రమే, అప్పటి నుండి వారి ఖాతాలో iss, subలను ఉంచండి. ఆ తర్వాత, వారిని iss, sub ద్వారా వెతకండి, ఇమెయిల్ ద్వారా ఎప్పుడూ కాదు: ఒక చిరునామా చేతులు మారవచ్చు, సబ్జెక్ట్ ఎప్పుడూ మారదు.