ક્વિકસ્ટાર્ટ: કોઈ પણ OIDC લાઇબ્રેરી
Usher એક સ્ટાન્ડર્ડ OpenID Connect પ્રોવાઇડર છે. જો તમારી ભાષામાં સર્ટિફાઇડ OIDC ક્લાયન્ટ હોય, તો તમારી પાસે જોઈતું બધું પહેલેથી છે: તેને તમારા એન્વાયર્નમેન્ટના ડિસ્કવરી ડૉક્યુમેન્ટ તરફ દોરો અને PKCE સાથે ઑથોરાઇઝેશન કોડ ફ્લો વાપરો.
સેટિંગ્સ
| સેટિંગ | વૅલ્યૂ |
|---|---|
| ઇશ્યુઅર | https://<handle>.usherid.com |
| ડિસ્કવરી URL | https://<handle>.usherid.com/.well-known/openid-configuration |
| ફ્લો | ઑથોરાઇઝેશન કોડ (response_type=code) |
| PKCE | જરૂરી, code_challenge_method=S256 |
| સ્કોપ | openid profile email offline_access |
| ક્લાયન્ટ ઑથેન્ટિકેશન | token_endpoint_auth_methods_supported માં જણાવ્યા મુજબ |
તમારા એન્વાયર્નમેન્ટનું હેન્ડલ Usher Console માં, તેના ઇશ્યુઅરની બાજુમાં છે. ડેવલપમેન્ટ એન્વાયર્નમેન્ટને wise-otter-42 જેવું રૅન્ડમ હેન્ડલ મળે છે; પ્રોડક્શન એન્વાયર્નમેન્ટમાં તમે પસંદ કરેલું હેન્ડલ હોય છે. પ્રોડક્શન એન્વાયર્નમેન્ટ તેના બદલે તમારા પોતાના ડોમેન પર પણ રહી શકે છે, જેમ કે login.acme.com: તમે CNAME રેકૉર્ડ વડે ડોમેનને Usher તરફ દોરો પછી એ જ તેનો ઇશ્યુઅર બને છે.
ડિસ્કવરી ડૉક્યુમેન્ટ વાંચો
તમારા ક્લાયન્ટને જોઈતો દરેક એન્ડપૉઇન્ટ અને કી અહીં સૂચિબદ્ધ છે. URL તમારા કોડમાં કૉપિ કરવાને બદલે તમારી લાઇબ્રેરીને તે વાંચવા દો.
curl -s https://acme.usherid.com/.well-known/openid-configuration | jq{
"issuer": "https://acme.usherid.com",
"authorization_endpoint": "https://acme.usherid.com/api/auth/oauth2/authorize",
"token_endpoint": "https://acme.usherid.com/api/auth/oauth2/token",
"userinfo_endpoint": "https://acme.usherid.com/api/auth/oauth2/userinfo",
"jwks_uri": "https://acme.usherid.com/api/auth/jwks",
"introspection_endpoint": "https://acme.usherid.com/api/auth/oauth2/introspect",
"revocation_endpoint": "https://acme.usherid.com/api/auth/oauth2/revoke",
"end_session_endpoint": "https://acme.usherid.com/api/auth/oauth2/end-session",
"scopes_supported": ["openid", "profile", "email", "offline_access"],
"response_types_supported": ["code"],
"grant_types_supported": ["authorization_code", "refresh_token", "client_credentials"],
"code_challenge_methods_supported": ["S256"]
}ફ્લો
વ્યક્તિને ઑથોરાઇઝેશન એન્ડપૉઇન્ટ પર મોકલો
એક રૅન્ડમ PKCE વેરિફાયર બનાવો અને તેને, રૅન્ડમ state સાથે, એવી જગ્યાએ રાખો જ્યાં ફક્ત આ બ્રાઉઝરનું સેશન પહોંચી શકે (ટૂંકા સમયની, httpOnly કૂકી સારી રીતે કામ કરે છે). પછી response_type=code, તમારા client_id અને redirect_uri, ઉપરના સ્કોપ, state, અને તમારા વેરિફાયરના S256 code_challenge સાથે રીડાયરેક્ટ કરો.
તેમને સાઇન ઇન કરવા દો
અહીંથી લૉબી સંભાળી લે છે: તે તમારી ઍપનું નામ બતાવે છે, તમારું એન્વાયર્નમેન્ટ મંજૂરી આપે છે તે સાઇન-ઇન રીતો આપે છે, અને પહેલી વાર સંમતિ માંગે છે.
કૉલબેક તપાસો
Usher code અને state સાથે તમારા redirect_uri પર રીડાયરેક્ટ કરે છે. state તમે રાખેલી state સાથે મેળ ન ખાય ત્યાં સુધી જવાબ નકારો. કોડ એક જ વાર વાપરી શકાય છે અને પાંચ મિનિટ ચાલે છે.
કોડના બદલામાં ટોકન લો
તમારા સર્વર પરથી, કોડ અને PKCE વેરિફાયર ટોકન એન્ડપૉઇન્ટ પર પોસ્ટ કરો:
curl -s -X POST https://acme.usherid.com/api/auth/oauth2/token \
-u "$CLIENT_ID:$CLIENT_SECRET" \
-d grant_type=authorization_code \
-d code="$CODE" \
-d redirect_uri=https://app.acme.example/callback \
-d code_verifier="$CODE_VERIFIER"ID ટોકન માન્ય કરો
jwks_uri પરની કી સામે તેની સિગ્નેચર તપાસો, અને એ પણ કે iss તમારો ઇશ્યુઅર છે, aud તમારું ક્લાયન્ટ ID છે અને exp ભવિષ્યમાં છે. તમારી લાઇબ્રેરી આ તમારા માટે કરે છે; ખાતરી કરો કે તે બંધ કરેલું નથી.
Node.js માં
openid-client ઉપરનું બધું કરે છે, માન્યતા તપાસ સહિત:
import * as client from 'openid-client'
// discovery: every endpoint and key comes from the issuer
const config = await client.discovery(
new URL('https://acme.usherid.com'),
process.env.USHER_CLIENT_ID!,
process.env.USHER_CLIENT_SECRET!,
)
// 1. send them to the Lobby, with PKCE (S256) and a state to check later
const codeVerifier = client.randomPKCECodeVerifier()
const state = client.randomState()
const url = client.buildAuthorizationUrl(config, {
redirect_uri: 'https://app.acme.example/callback',
scope: 'openid profile email offline_access',
code_challenge: await client.calculatePKCECodeChallenge(codeVerifier),
code_challenge_method: 'S256',
state,
})
// keep codeVerifier and state in a short-lived httpOnly cookie, then redirect to url
// 2. back at /callback: swap the code for tokens
const tokens = await client.authorizationCodeGrant(config, new URL(request.url), {
pkceCodeVerifier: codeVerifier,
expectedState: state,
})
const { sub, email, name } = tokens.claims()!બીજી ભાષાઓમાં
આ વ્યાપક રીતે વપરાતી લાઇબ્રેરી બધી સ્ટાન્ડર્ડ OpenID Connect બોલે છે અને Usher માટે ખાસ કંઈ કર્યા વિના Usher સાથે કામ કરે છે:
| ભાષા | લાઇબ્રેરી |
|---|---|
| Python | Authlib |
| Go | coreos/go-oidc, golang.org/x/oauth2 સાથે |
| Java | Spring Security OAuth 2.0 Client |
| .NET | Microsoft.AspNetCore.Authentication.OpenIdConnect |
| Ruby | omniauth_openid_connect |
| PHP | jumbojett/openid-connect-php |
સાઇન ઇન રહેવું
offline_access સ્કોપ સાથે, ટોકનના જવાબમાં રિફ્રેશ ટોકન હોય છે. જૂનું ઍક્સેસ ટોકન સમાપ્ત થાય ત્યારે નવું મેળવવા માટે તેને grant_type=refresh_token સાથે વાપરો. રિફ્રેશ ટોકન રોટેટ થાય છે: દરેક ઉપયોગ નવું ટોકન આપે છે, અને જૂનું કામ કરતું બંધ થઈ જાય છે.
સાઇન આઉટ
પહેલાં તમારું પોતાનું સેશન સાફ કરો, પછી post_logout_redirect_uri (અને જો હોય તો id_token_hint) સાથે end_session_endpoint પર રીડાયરેક્ટ કરો, જેથી લૉબી પણ વ્યક્તિને ભૂલી જાય.