Class: Stytch::OAuth
- Inherits:
-
Object
- Object
- Stytch::OAuth
- Includes:
- RequestHelper
- Defined in:
- lib/stytch/oauth.rb
Instance Method Summary collapse
-
#attach(provider:, user_id: nil, session_token: nil, session_jwt: nil) ⇒ Object
Generate an OAuth Attach Token to pre-associate an OAuth flow with an existing Stytch User.
-
#authenticate(token:, session_token: nil, session_duration_minutes: nil, session_jwt: nil, session_custom_claims: nil, code_verifier: nil, telemetry_id: nil) ⇒ Object
Authenticate a User given a
token. -
#initialize(connection) ⇒ OAuth
constructor
A new instance of OAuth.
Methods included from RequestHelper
#delete_request, #get_request, #post_request, #put_request, #request_with_query_params
Constructor Details
#initialize(connection) ⇒ OAuth
Returns a new instance of OAuth.
15 16 17 |
# File 'lib/stytch/oauth.rb', line 15 def initialize(connection) @connection = connection end |
Instance Method Details
#attach(provider:, user_id: nil, session_token: nil, session_jwt: nil) ⇒ Object
Generate an OAuth Attach Token to pre-associate an OAuth flow with an existing Stytch User. Pass the returned oauth_attach_token to the same provider's OAuth Start endpoint to treat this OAuth flow as a login for that user instead of a signup for a new user.
Exactly one of user_id, session_token, or session_jwt must be provided to identify the target Stytch User.
Note: This is an optional step in the OAuth flow. Stytch can often determine whether to associate a new OAuth login with an existing User based on verified information (such as an email address) from the identity provider. This endpoint is useful for cases where we can't, such as missing or unverified provider information.
See our OAuth email address behavior resource for additional information.
Parameters:
- provider
The OAuth provider's name. The type of this field is
String.- user_id
The unique ID of a specific User. You may use an
external_idhere if one is set for the user. The type of this field is nilableString.- session_token
The
session_tokenassociated with a User's existing Session. The type of this field is nilableString.- session_jwt
The
session_jwtassociated with a User's existing Session. The type of this field is nilableString.
Returns:
An object with the following fields:
- request_id
Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue. The type of this field is
String.- oauth_attach_token
A single-use token for connecting the Stytch User selection from an OAuth Attach request to the corresponding OAuth Start request. The type of this field is
String.- status_code
The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors. The type of this field is
Integer.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/stytch/oauth.rb', line 52 def attach( provider:, user_id: nil, session_token: nil, session_jwt: nil ) headers = {} request = { provider: provider } request[:user_id] = user_id unless user_id.nil? request[:session_token] = session_token unless session_token.nil? request[:session_jwt] = session_jwt unless session_jwt.nil? post_request('/v1/oauth/attach', request, headers) end |
#authenticate(token:, session_token: nil, session_duration_minutes: nil, session_jwt: nil, session_custom_claims: nil, code_verifier: nil, telemetry_id: nil) ⇒ Object
Authenticate a User given a token. This endpoint verifies that the user completed the OAuth flow by verifying that the token is valid and hasn't expired. To initiate a Stytch session for the user while authenticating their OAuth token, include session_duration_minutes; a session with the identity provider, e.g. Google or Facebook, will always be initiated upon successful authentication.
Parameters:
- token
The OAuth
tokenfrom the?token=query parameter in the URL.
The redirect URL will look like `https://example.com/authenticate?stytch_token_type=oauth&token=rM_kw42CWBhsHLF62V75jELMbvJ87njMe3tFVj7Qupu7`
In the redirect URL, the `stytch_token_type` will be `oauth`. See [here](https://stytch.com/docs/workspace-management/redirect-urls) for more detail.
The type of this field is +String+.
- session_token
Reuse an existing session instead of creating a new one. If you provide us with a
session_token, then we'll update the session represented by this session token with this OAuth factor. If thissession_tokenbelongs to a different user than the OAuth token, the session_jwt will be ignored. This endpoint will error if bothsession_tokenandsession_jwtare provided. The type of this field is nilableString.- session_duration_minutes
Set the session lifetime to be this many minutes from now. This will start a new session if one doesn't already exist, returning both an opaque
session_tokenandsession_jwtfor this session. Remember that thesession_jwtwill have a fixed lifetime of five minutes regardless of the underlying session duration, and will need to be refreshed over time.
This value must be a minimum of 5 and a maximum of 527040 minutes (366 days).
If a `session_token` or `session_jwt` is provided then a successful authentication will continue to extend the session this many minutes.
If the `session_duration_minutes` parameter is not specified, a Stytch session will not be created.
The type of this field is nilable +Integer+.
- session_jwt
Reuse an existing session instead of creating a new one. If you provide us with a
session_jwt, then we'll update the session represented by this JWT with this OAuth factor. If thissession_jwtbelongs to a different user than the OAuth token, the session_jwt will be ignored. This endpoint will error if bothsession_tokenandsession_jwtare provided. The type of this field is nilableString.- session_custom_claims
Add a custom claims map to the Session being authenticated. Claims are only created if a Session is initialized by providing a value in
session_duration_minutes. Claims will be included on the Session object and in the JWT. To update a key in an existing Session, supply a new value. To delete a key, supply a null value.
Custom claims made with reserved claims ("iss", "sub", "aud", "exp", "nbf", "iat", "jti") will be ignored. Total custom claims size cannot exceed four kilobytes.
The type of this field is nilable object.
- code_verifier
A base64url encoded one time secret used to validate that the request starts and ends on the same device. The type of this field is nilable
String.- telemetry_id
If the
telemetry_idis passed, as part of this request, Stytch will call the Fingerprint Lookup API and store the associated fingerprints and IPGEO information for the User. Your workspace must be enabled for Device Fingerprinting to use this feature. The type of this field is nilableString.
Returns:
An object with the following fields:
- request_id
Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue. The type of this field is
String.- user_id
The unique ID of the affected User. The type of this field is
String.- provider_subject
The unique identifier for the User within a given OAuth provider. Also commonly called the "sub" or "Subject field" in OAuth protocols. The type of this field is
String.- provider_type
Denotes the OAuth identity provider that the user has authenticated with, e.g. Google, Facebook, GitHub etc. The type of this field is
String.- session_token
A secret token for a given Stytch Session. The type of this field is
String.- session_jwt
The JSON Web Token (JWT) for a given Stytch Session. The type of this field is
String.- provider_values
The
provider_valuesobject lists relevant identifiers, values, and scopes for a given OAuth provider. For example this object will include a provider'saccess_tokenthat you can use to access the provider's API for a given user.
Note that these values will vary based on the OAuth provider in question, e.g. id_token is only returned by OIDC compliant identity providers.
The type of this field is ProviderValues (+object+).
- user
The
userobject affected by this API call. See the Get user endpoint for complete response field details. The type of this field isUser(+object+).- reset_sessions
Indicates if all other of the User's Sessions need to be reset. You should check this field if you aren't using Stytch's Session product. If you are using Stytch's Session product, we revoke the User's other sessions for you. The type of this field is
Boolean.- oauth_user_registration_id
The unique ID for an OAuth registration. The type of this field is
String.- status_code
The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors. The type of this field is
Integer.- user_session
A
Sessionobject. For backwards compatibility reasons, the session from an OAuth authenticate call is labeled asuser_session, but is otherwise just a standard stytchSessionobject.
See Session object for complete response fields.
The type of this field is nilable +Session+ (+object+).
- user_device
If a valid
telemetry_idwas passed in the request and the Fingerprint Lookup API returned results, theuser_deviceresponse field will contain information about the user's device attributes. The type of this field is nilableDeviceInfo(+object+).
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/stytch/oauth.rb', line 154 def authenticate( token:, session_token: nil, session_duration_minutes: nil, session_jwt: nil, session_custom_claims: nil, code_verifier: nil, telemetry_id: nil ) headers = {} request = { token: token } request[:session_token] = session_token unless session_token.nil? request[:session_duration_minutes] = session_duration_minutes unless session_duration_minutes.nil? request[:session_jwt] = session_jwt unless session_jwt.nil? request[:session_custom_claims] = session_custom_claims unless session_custom_claims.nil? request[:code_verifier] = code_verifier unless code_verifier.nil? request[:telemetry_id] = telemetry_id unless telemetry_id.nil? post_request('/v1/oauth/authenticate', request, headers) end |