Skip to content

Commit

Permalink
Merge pull request #18 from StreamAMG/release/1.2.5
Browse files Browse the repository at this point in the history
SCD-101 Token Custom SSO (Production)
  • Loading branch information
StefanoStream authored Oct 17, 2023
2 parents df9d40c + f7bdf2a commit ddcd04f
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 11 deletions.
77 changes: 74 additions & 3 deletions AuthenticationReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ StreamSDK Authentication SDK
The StreamSDK Authentication SDK is a light wrapper around the authentication API and provides:

* Logging in via email and password
* Logging in using custom SSO
* Getting a token
* Getting a KSession
* Storing credentials and logging in silently
Expand Down Expand Up @@ -49,9 +50,10 @@ authenticationSdk.initWithURL("https://my.client.url.payments/", "lang=" + Local

`initWithURL` takes 2 parameters: the base url of the service, and a second optional parameter string which is appended to all requests

## Usage
Logging In
========
To authenticate with the selected StreamAMG Authentication API, simply pass an email and password to the login function, with a callback block

Once initialised, call the functions of the SDK with callbacks:
```
authenticationSdk.login(email, password) { result ->
when (result) {
Expand All @@ -61,13 +63,52 @@ authenticationSdk.login(email, password) { result ->
}
```

Alternatively, it is also possible to automatically login a previously verified user as long as they have not since logged out:

```
authenticationSdk.loginSilent { result ->
when (result) {
is LoginResult.LoginOK -> onloginSuccess()
else -> handleLoginError(result)
}
}
```

Logging Out
=========
To end a user's session and remove the user's credentials from the Keychain, you can log the user out:

```
authenticationSdk.logout { result ->
when (result) {
is LogoutResult.LogoutOK -> onlogoutSuccess()
else -> handleLogoutError(result)
}
}
```

Requesting Key Session Token
=======================
If the Authentication API is providing Key Session Tokens, these can also be requested via the SDK:

```
authenticationSdk.getKS(entryID) { keySessionResult ->
if (keySessionResult is GetKeySessionResult.Granted) {
// Valid KS
KS = keySessionResult.keySession
} else {
// Manage other GetKeySessionResult type
}
}
```

Using custom SSO
=======================
Authentication SDK supports custom SSO implementation that are correctly configured with CloudPay and are able to generate a valid use JWT token.

## Start the session with custom SSO

In order to comunicate with CloudPay the app needs to start the session with the users token.
In order to communicate with CloudPay the app needs to start the session with the users token generated. This is an optional step and required only if you are not using cloud pay login but you want to start a user session to log the sessions and so get CloudPay check the concurrency.

```
authenticationSdk.startSession("jwtToken",
Expand Down Expand Up @@ -110,6 +151,7 @@ authenticationSdk.updateUserSummary(firstname,lastname, token
}
)
```

## Retrieve user summary
The retrieve user summary function allows a user to retrieve their user details if the user has a valid token.

Expand All @@ -125,6 +167,35 @@ authenticationSdk.getUserSummary(token
}
)
```

## Logout with custom SSO

If you are using custom SSO, to logout from cloudpay, use this method to logout by passing the token you previously used to start the SSO.

```
authenticationSdk.logoutWithToken(token) { result ->
when (result) {
is LogoutResult.LogoutOK -> onlogoutSuccess()
else -> handleLogoutError(result)
}
}
```

## GetKS with custom SSO

If you are using custom SSO, then to get the user entitlements use this method. Please pass the same token you used to start the custom SSO session.

```
authenticationSdk.getKSWithToken(token, entryID) { keySessionResult ->
if (keySessionResult is GetKeySessionResult.Granted) {
// Valid KS
KS = keySessionResult.keySession
} else {
// Manage other GetKeySessionResult type
}
}
```

Change Log:
===========

Expand Down
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Change Log:

All notable changes to this project will be documented in this section.

### 1.2.5 - Updated Authentication module with new methods to logout and getKS with third party JWT tokens.

### 1.2.4 - Updated MediaType APIs from POST to GET

### 1.2.3 - Fixed StreamPlay FixtureID field types
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ plugins {
id 'maven-publish'
}
// Versions of the library modules are matched (eg 1.0 core goes with 1.0 cloudmatrix) to prevent dependency issues
ext.SDK_VERSION_CODE = 24
ext.SDK_VERSION_NAME = "1.2.4"
ext.SDK_VERSION_CODE = 25
ext.SDK_VERSION_NAME = "1.2.5"

publishing {
publications {
Expand Down
1 change: 1 addition & 0 deletions streamamg-sdk-authentication/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
}

android {
namespace = "com.streamamg.streamamg_sdk_authentication"
compileSdkVersion 33

defaultConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ interface AuthenticationSDK {
@Deprecated("Use login with callback which returns the token")
fun loginSyncToken(email: String, password: String): String
fun getKS(entryID: String, callback: (GetKeySessionResult) -> Unit)
fun getKSWithToken(token: String, entryID: String, callback: (GetKeySessionResult) -> Unit)
fun logout(callback: (LogoutResult) -> Unit)
fun logoutWithToken(token: String, callback: (LogoutResult) -> Unit)
fun startSession(token: String, onSuccess: () -> Unit,onError: (Error) -> Unit)
fun updateUserSummary(
firstName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import timber.log.Timber

internal class StreamAMGAuthenticationSDK(
private val coreSDK: StreamAMGSDK,
@VisibleForTesting var api: AuthenticationApi? = null
@get:VisibleForTesting var api: AuthenticationApi? = null
) : AuthenticationSDK {

private var apiUrl: String? = null
Expand Down Expand Up @@ -80,10 +80,21 @@ internal class StreamAMGAuthenticationSDK(
}

override fun logout(callback: (LogoutResult) -> Unit) {
logoutWithToken(sessionId, callback)
}

/**
Logs out a user by sending a logout request with the provided token.
- Parameters:
- token: The authentication token for the user.
- callback: A closure to be called upon completion of the logout operation. It takes a `LogoutResult` type as an argument.
- Note: Make sure the `url` property is properly set before calling this method.
This function sends a logout request to the authentication API and handles the response accordingly.
*/
override fun logoutWithToken(token: String, callback: (LogoutResult) -> Unit) {
clearSavedCredentials()

api?.logout(paramsMap, sessionId)
api?.logout(paramsMap, token)
?.applySchedulers()
?.subscribeBy(
onSuccess = {
Expand Down Expand Up @@ -217,7 +228,7 @@ internal class StreamAMGAuthenticationSDK(
return Single.create<String> {
login(email, password) { result ->
if (result is LoginResult.LoginOK && loginResponse?.authenticationToken != null) {
it.onSuccess(loginResponse!!.authenticationToken)
it.onSuccess(loginResponse!!.authenticationToken!!)
} else {
loginResponse = LoginResponse()
loginResponse?.authenticationToken = null
Expand Down Expand Up @@ -281,12 +292,25 @@ internal class StreamAMGAuthenticationSDK(
}

override fun getKS(entryID: String, callback: (GetKeySessionResult) -> Unit) {
getKSWithToken(sessionId, entryID, callback)
}

/**
Retrieves a Key Session (KS) for a specific entry using the provided token.
- Parameters:
- token: The authentication token for the user.
- entryID: The ID of the entry for which the KS is requested.
- callback: A closure to be called upon completion of the KS retrieval operation. It takes a `GetKeySessionResult` type as an argument.
- Note: Ensure that the `url` property is properly set before calling this method.
This function sends a request to retrieve a Key Session (KS) for a specific entry and handles the response accordingly.
*/
override fun getKSWithToken(token: String, entryID: String, callback: (GetKeySessionResult) -> Unit) {
if (!isInitialized()) {
callback.invoke(GetKeySessionResult.Error(Exception("PaymentSDK not initialized")))
return
}

api?.getKS(entryID, sessionId, paramsMap)
api?.getKS(entryID, token, paramsMap)
?.applySchedulers()
?.subscribeBy(
onSuccess = { response ->
Expand All @@ -299,7 +323,6 @@ internal class StreamAMGAuthenticationSDK(
callback.invoke(GetKeySessionResult.Error(it))
})
?.addTo(disposable)

}

private fun handleKSLoginResponse(loginResponse: LoginResponse, callback: (GetKeySessionResult) -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Single
import io.reactivex.rxjava3.schedulers.Schedulers

fun <T> Single<T>.applySchedulers(): Single<T> {
fun <T : Any> Single<T>.applySchedulers(): Single<T> {
return subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
}
1 change: 1 addition & 0 deletions streamamg-sdk-cloudmatrix/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
}

android {
namespace = "com.streamamg.streamamg_sdk_cloudmatrix"
compileSdkVersion 33

defaultConfig {
Expand Down
1 change: 1 addition & 0 deletions streamamg-sdk-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
}

android {
namespace = "com.streamamg.streamapi_core"
compileSdkVersion 33

defaultConfig {
Expand Down
1 change: 1 addition & 0 deletions streamamg-sdk-playkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
}

android {
namespace = "com.streamamg.amg_playkit"
compileSdkVersion 33

defaultConfig {
Expand Down
1 change: 1 addition & 0 deletions streamamg-sdk-playkit2go/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
}

android {
namespace = "com.streamamg.streamamg_sdk_playkit2go"
compileSdkVersion 33

defaultConfig {
Expand Down
1 change: 1 addition & 0 deletions streamamg-sdk-purchases/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
}

android {
namespace = "com.streamamg.streamamg_sdk_purchases"
compileSdkVersion 33

defaultConfig {
Expand Down
1 change: 1 addition & 0 deletions streamamg-sdk-streamplay/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
}

android {
namespace = "com.streamamg.streamapi_streamplay"
compileSdkVersion 33

defaultConfig {
Expand Down

0 comments on commit ddcd04f

Please sign in to comment.