A mobile app that signs in with the provider’s own SDK (Credential Manager or
Google Sign-In, Sign in with Apple) receives an ID token and no authorization
code, so it cannot use the browser callback. Only the Google
and Apple presets support native login; every other preset returns
ErrUnsupported. The examples assume the started client, guard and mapper
from Browser Login.
Mount Routes
Native login uses two routes on the same session, which the app keeps in its cookie jar. Both use the default session of the session middleware. Mount them behind the same rate limit as your other login routes:
web.Get("/auth/google/nonce", client.NonceHandler(), oauthauth.NonceOperation)
web.Post("/auth/google/native", client.NativeHandler(guard, mapIdentity), oauthauth.NativeOperation)
NonceHandler answers {"nonce": "..."} and stores the nonce in the session.
The nonce is valid for ten minutes and one attempt spends it. The app gives the
nonce to the SDK, then posts {"id_token": "..."} to NativeHandler. That
handler verifies the token’s signature, expiry, issuer, audience and nonce,
calls your mapper and guard.Begin, and answers like every other login method:
204 when the person is logged in, or 202 with the second-factor methods to
choose from (Hold a Login). The
session ID rotates, and a completed login publishes LoginSucceeded. Native
login needs no challenge URL, because the 202 response carries the methods.
What the app passes to the SDK depends on the provider. Google returns the nonce
unchanged in the token’s nonce claim, so the app passes it as received. Apple
puts the request nonce into the token unchanged, and this client requires the
lowercase hexadecimal SHA-256 of the issued nonce, so the token never carries the
secret held in the session; any other form is rejected.
A failed attempt, including a wrong audience, costs the app a new nonce request.
A missing, expired or used nonce, a bad token or a nonce mismatch returns
ErrInvalidCallback and publishes LoginFailed. A malformed body or an empty
id_token returns ErrInvalidLoginRequest without spending the nonce.
Accept Audiences
An audience names a token’s intended recipient. List the audiences your apps
request tokens for with ClientBuilder.NativeAudiences. Without them the
handlers return ErrNoNativeAudiences, and a token for any other audience,
including the browser client ID, is rejected:
client, err := oauth.New(oauth.Google(), cipher, webClientID,
func(login *oauth.ClientBuilder) {
login.NativeAudiences(webClientID, iosClientID)
},
)
- Google. The ID token’s audience is the server client ID the app configures
in its SDK: Credential Manager’s
setServerClientIdon Android andGIDConfiguration.serverClientIDon iOS. That is normally the web client ID, the one you giveoauth.New. List it, and any iOS or Android client ID your apps request tokens for. - Apple. The
audis the app’sclient_id, which for a native app is its bundle ID, not the Services ID of browser login. Apple’s token carries no name: the SDK hands it to the app on the first authorization only, so the app sends it to your API itself.
Email and EmailVerified follow the browser rules: Google’s boolean
email_verified and Apple’s email_verified (a boolean or "true"). A native
login sets no Claims["scope"], because no authorization code is exchanged.
These facts come from the provider documentation:
- Google: Authenticate with a backend server
and OpenID Connect
for
iss,aud,exp,email_verified,hdand thenonceclaim;GetGoogleIdOption.BuilderandGIDConfigurationfor the server client ID as token audience and the nonce. - Apple: Verifying a user
(verify the nonce,
iss,audas the developer’sclient_idandexp) and the identity token claims (nonce,nonce_supported,email_verifiedas a string or Boolean).
Native ID tokens are not accepted by Verify, which
requires API audiences and rejects tokens that carry a nonce. To trade a native
login for a device token, check
guard.RecentLogin first.
For all functions and options, see the
oauthauth API reference.