Search…
Authentication
The Sudo Platform allows users to sign in from client apps using various mechanisms. Once signed in, a user obtains authentication tokens (OpenID Connect ID and access tokens) that can be used to access other services within the platform.

OIDC Federated Sign In

In order to use federated sign in, you must have completed the federated sign in setup for your project by contacting [email protected].
If you are using an external identity provider that supports OpenID Connect (OIDC) standard then Sudo Platform access can be granted via federated sign in. When the user signs into your identity provider they will be federated and authenticated into Sudo Platform via OpenID Connect web flow. The user is not required to sign into your identity provider again until the refresh token expires. The refresh token lifetime can be configured by contacting [email protected].
The federated sign in requires the app to be configured with a URL scheme so that the authentication tokens from your identity provider can be passed into your app when the user performs federated sign in via a web view launched by the app. The URL scheme that you set up for your app must match one of the callback and logout URLs configured in the federated sign in configuration. To setup these URLs, contact [email protected].
If you have more than one URL scheme configured for callback and logout URLs then make sure you edit "sudoplatformconfig.json" file to keep only the URL scheme that's relevant for your app for "signInRedirectUri" and "signOutRedirectUri".
Setting up a URL scheme for iOS app:
Apple Developer Documentation
For example, add the following entry in Info.plist file of your app.
1
<key>CFBundleURLTypes</key>
2
<array>
3
<dict>
4
<key>CFBundleURLSchemes</key>
5
<array>
6
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
7
</array>
8
</dict>
9
</array>
Copied!
In addition, add the following code in your AppDelegate to process the redirect URL that launches your app via the URL scheme.
1
do {
2
client.processFederatedSignInTokens(url: url)
3
} catch let error {
4
// Handle error. An error might be thrown for unrecoverable circumstances arising
5
// from programmatic error or configuration error. For example, if the federated
6
// sign in is not configured in your environment then `invalidConfig` error might
7
// be thrown.
8
}
Copied!
Setting up a URL scheme for Android app:
Create Deep Links to App Content  |  Android Developers
Android Developers
For example, add the following to your app manifest in the main activity section:
1
<intent-filter>
2
<action android:name="android.intent.action.VIEW" />
3
<category android:name="android.intent.category.DEFAULT" />
4
<category android:name="android.intent.category.BROWSABLE" />
5
<data android:scheme="com.mycompany.myapp"/>
6
</intent-filter>
Copied!
Signing in via federated sign in.
Swift
Kotlin
TypeScript
1
do {
2
// The current view should have a navigation controller since the federated
3
// sign in requires a web view to be launched to browse to your identity
4
// provider.
5
try client.presentFederatedSignInUI(presentationAnchor: window) { (result) in
6
switch result {
7
case .success:
8
// Registration and sign in successful.
9
case .failure(let cause):
10
// Handle error. A failure result may be returned if the backend is unable
11
// perform the registration due to availability or security issues.
12
}
13
}
14
} catch let error {
15
// Handle error. An error might be thrown for unrecoverable circumstances arising
16
// from programmatic error or configuration error. For example, if the keychain
17
// access entitlement is not set up correctly, register API is getting called
18
// multiple times simultaneously or basic system resources are unavailable.
19
}
Copied!
1
// Your main activity must pass the authentication tokens from your
2
// identity provider into SudoUserClient when the activity is resumed
3
// via the URL scheme.
4
override fun onResume() {
5
super.onResume()
6
7
val data = this.intent.data
8
if (data != null) {
9
client.processFederatedSignInTokens(data)
10
}
11
}
12
13
// Present the federated sign in UI.
14
client.presentFederatedSignInUI { result ->
15
when (result) {
16
is ApiResult.Success -> {
17
// User registered and signed in successfully.
18
}
19
is ApiResult.Failure -> {
20
// Handle error. A failure result may be returned if the backend is unable
21
// perform the registration due to availability or security issues.
22
}
23
}
24
}
Copied!
1
try {
2
client.presentFederatedSignInUI()
3
// User registered and signed in successfully.
4
catch (err) {
5
// Handle error. A failure result may be returned if the backend is unable
6
// perform the registration due to availability or security issues.
7
}
8
9
// In addition to the above, you will need to add code to process the redirect url.
10
processRedirectUrl = async () => {
11
const redirectUrl = window.location.href
12
await client.processFederatedSignInTokens(redirectUrl)
13
}
Copied!
See it in action. Be sure to take a look at the iOS and Android sample apps on GitHub, which demonstrate how to use federated sign in.

Custom Federated Sign In

In order to use custom federated sign in, you must have completed the custom federated sign in setup described in the Registration section.
In order to sign in using a custom authentication provider, you must invoke signInWithAuthenticationProvider API of SudoUserClient with your AuthenticationProvider implementation as an input similar to how registerWithAuthenticationProvider was invoked to register a new Sudo Platform user. For details of AuthenticationProvider implementation, please refer to the Registration section and API Reference.

Sign In via Private Key

If a user was registered using Sign in Key Registration, they can use the private key from the public/private key pair generated at registration time to digitally sign an authentication token to sign-in.
To sign-in using private key based authentication:
Swift
Kotlin
1
do {
2
try client.signInWithKey() { (result) in
3
switch result {
4
case .success(let idToken, let accessToken, let refreshToken, let lifetime):
5
// "idToken" can be used to initialize other service specific clients to authenticate to the backend API.
6
// ID token and access token will expire after number of seconds specified in "lifetime" so use "refreshToken"
7
// to refresh these tokens via "refreshTokens" API.
8
case .failure(let cause):
9
// Handle error. A failure result may be returned if the backend is unable
10
// perform the sign in due to availability or security issues.
11
}
12
}
13
} catch let error {
14
// Handle error. An error might be thrown for unrecoverable circumstances arising
15
// from programmatic error or configuration error. For example, if the keychain
16
// access entitlement is not set up correctly, the client is not registered,
17
// or basic system resources are unavailable.
18
}
Copied!
1
CoroutineScope(Dispatchers.IO).launch {
2
try {
3
val result = client.signInWithKey()
4
// "idToken" can be used to initialize other service specific clients to authenticate to the backend API.
5
// ID token and access token will expire after number of seconds specified in "lifetime" so use "refreshToken"
6
// to refresh these tokens via "refreshTokens" API.
7
println("idToken: ${result.idToken}, accessToken: ${result.accessToken}, refreshToken: ${result.refreshToken}")
8
} catch (e: AuthenticationException) {
9
// Handle error. A failure result may be returned if the backend is unable
10
// to perform the sign in due to availability or security issues.
11
println("${e.localizedMessage}")
12
}
13
}
Copied!

Refreshing ID and Access Tokens

Authentication tokens will expire after 1 hour so in order to avoid needing to sign in periodically you should refresh those tokens.
Each Sudo Platform client such as Telephony client will automatically refresh ID and access tokens as needed but this will incur a delay in the API call so it is recommended to refresh the tokens at a scheduled interval or on app launch.
To refresh ID and access tokens:
Swift
Kotlin
TypeScript
1
do {
2
try client.refreshTokens(refreshToken: refreshToken) { (result) in
3
switch result {
4
case .success(let idToken, let accessToken, let refreshToken, let lifetime):
5
// Received fresh set of tokens.
6
case .failure(let cause):
7
// Handle error. A failure result may be returned if the backend is unable
8
// perform the refresh due to availability or security issues.
9
}
10
}
11
} catch let error {
12
// Handle error. An error might be thrown for unrecoverable circumstances arising
13
// from programmatic error or configuration error. For example, if the keychain
14
// access entitlement is not set up correctly, the client is not signed in,
15
// or basic system resources are unavailable.
16
}
Copied!
1
CoroutineScope(Dispatchers.IO).launch {
2
try {
3
val result = client.refreshTokens(refreshToken)
4
// "idToken" can be used to initialize other service specific clients to authenticate to the backend API.
5
// ID token and access token will expire after number of seconds specified in "lifetime" so use "refreshToken"
6
// to refresh these tokens via "refreshTokens" API.
7
println("idToken: ${result.idToken}, accessToken: ${result.accessToken}, refreshToken: ${result.refreshToken}")
8
} catch (e: AuthenticationException) {
9
// Handle error. A failure result may be returned if the backend is unable
10
// to perform the sign in due to availability or security issues.
11
println("${e.localizedMessage}")
12
}
13
}
Copied!
1
try {
2
const result = await client.refreshTokens(refreshToken)
3
// "idToken" can be used to initialize other service specific clients to authenticate to the backend API.
4
// ID token and access token will expire at the time indicated in tokenExpiry so use "refreshToken"
5
// to refresh these tokens via the "refreshTokens" API.
6
} catch (err) {
7
// Handle error. A failure result may be returned if the backend is unable
8
// to perform the sign in due to availability or security issues.
9
}
Copied!
It is not necessary to perform the sign in again until the refresh token expires as long as the identity token is getting refreshed via the mechanisms mentioned above. Currently, it is not possible to determine the refresh token's expiry programmatically so it is the consumer's responsibility to perform the sign in at a fixed interval or reactively when notAuthorized error is returned from a platform service client API call. The default expiry for refresh token is 30 days.

Sign Out

To sign out a user:
Swift
Kotlin
TypeScript
1
do {
2
try self.client.globalSignOut { (result) in
3
switch result {
4
case .success:
5
// Sign out successful.
6
case .failure(let cause):
7
// Handle error. A failure result may be returned if the backend is unable
8
// perform the refresh due to availability or security issues.
9
}
10
}
11
} catch let error {
12
// Handle error. An error might be thrown for unrecoverable circumstances arising
13
// from programmatic error or configuration error. For example, if the keychain
14
// access entitlement is not set up correctly, the client is not signed in,
15
// or basic system resources are unavailable.
16
}
Copied!
1
CoroutineScope(Dispatchers.IO).launch {
2
try {
3
client.globalSignOut()
4
// Sign out successful.
5
} catch (e: SignOutException) {
6
// Handle error. A failure result may be returned if the backend is unable
7
// perform the sign out due to availability or security issues.
8
println("${e.localizedMessage}")
9
}
10
}
Copied!
1
try {
2
await client.globalSignOut()
3
// Sign out successful.
4
} catch (err) {
5
// Handle error. A failure result may be returned if the backend is unable
6
// to perform the sign out due to availability or security issues.
7
}
Copied!
Sign out will invalidate authentication tokens from all devices regardless of the sign in method used. For the current device, all tokens will be invalidated immediately but on other devices the API access will still be granted to up to the lifetime of ID token. The ID token lifetime is 1 hour by default but this can be changed by submitting a support request.
To invalidate authentication tokens for a single device.
Swift
Kotlin
TypeScript
1
do {
2
try self.client.clearAuthTokens()
3
} catch let error {
4
// Handle error. An error might be thrown for unrecoverable circumstances arising
5
// from programmatic error or configuration error. For example, if the keychain
6
// access entitlement is not set up correctly, the client is not signed in,
7
// or basic system resources are unavailable.
8
}
Copied!
1
client.clearAuthTokens()
Copied!
1
client.clearAuthenticationTokens()
Copied!
Sign out from hosted UI for OIDC federated sign in.
Swift
Kotlin
TypeScript
1
do {
2
try client.presentFederatedSignOutUI(presentationAnchor: window) { (result) in
3
switch result {
4
case .success:
5
// Sign out successful.
6
case .failure(let cause):
7
// Handle error. A failure result may be returned if the backend is unable
8
// perform the sign out due to availability or security issues.
9
}
10
}
11
} catch let error {
12
// Handle error. An error might be thrown for unrecoverable circumstances arising
13
// from programmatic error or configuration error.
14
}
Copied!
1
client.presentFederatedSignOutUI { result ->
2
when (result) {
3
is ApiResult.Success -> {
4
// User signed out successfully.
5
}
6
is ApiResult.Failure -> {
7
// Handle error. A failure result may be returned if the backend is unable
8
// perform the sign out due to availability or security issues.
9
}
10
}
11
}
Copied!
1
client.presentSignOutUI()
Copied!
This will invalidated any cached information from the web session used to contact Sudo Platform but does not invalidate any authentication tokens. You can call clearAuthTokens or globalSignOut subsequently to either invalidate authentication tokens from the current device or sign out the user from all devices.
Last modified 1d ago