Cell - OAuth2 Client Component
Sign users in with an OAuth 2.0 / OpenID Connect provider (GitHub, Google, Okta, Facebook or any other), and get access tokens for calling APIs with the client-credentials, password and refresh-token grants. It plugs into @celljs/security: a successful login is a normal security login.
bash
npm install @celljs/oauth2-client @celljs/securityLogin with a provider
Register the client in cell.yml (server-only, like all configuration):
yaml
oauth2:
client:
registrations:
github:
clientId: "${env.GITHUB_CLIENT_ID}"
clientSecret: "${env.GITHUB_CLIENT_SECRET}"google,github,facebookandoktahave templates (registrationTemplates,providerTemplatesin this package'scell.yml) with their endpoints and scopes; Okta and other providers need their URLs underoauth2.client.providers.<id>(authorizationUri,tokenUri,userInfoEndpoint.uri,userInfoEndpoint.userNameAttributeName,jwkSetUri,issuerUri).- A registration has
clientId,clientSecret,scopes,authorizationGrantType,clientAuthenticationMethod,redirectUri,provider(default: its id) andclientName. - Send the browser to
/oauth2/authorization/github(oauth2.client.authorizationRequestBaseUri). The provider redirects back to/login/oauth2/code/github; the default redirect URI is built fromserver.endpoint, so setserver.endpointto the public URL of the application. - With the
openidscope the login is OpenID Connect: the ID token is verified with the provider's JWK set (@celljs/oauth2-jose). - The user's attributes are mapped by
security.userMapperRule.<provider>of@celljs/security.
Tokens for API calls
AuthorizedClientManager authorizes a registration with the grant it declares (client_credentials, password, refresh_token, authorization_code) and keeps the tokens, refreshing them oauth2.client.clockSkew (60 s) before they expire.