From 936359453d4ff44830896db18413e3d819163cc2 Mon Sep 17 00:00:00 2001
From: Zachariah Tom <zachariahtom@gmail.com>
Date: Mon, 9 Oct 2023 17:32:20 +0100
Subject: [PATCH 1/2] Updated Code to process ks and logout with third party
 token

---
 .../AuthenticationSDK.swift                   | 66 +++++++++++++++----
 1 file changed, 54 insertions(+), 12 deletions(-)

diff --git a/Source/StreamSDKAuthentication/AuthenticationSDK.swift b/Source/StreamSDKAuthentication/AuthenticationSDK.swift
index a8e11df..4b331bf 100644
--- a/Source/StreamSDKAuthentication/AuthenticationSDK.swift
+++ b/Source/StreamSDKAuthentication/AuthenticationSDK.swift
@@ -112,28 +112,50 @@ public class AuthenticationSDK {
      * @param completion Completion block capturing StreamAMGUserModel or StreamAMGError
      */
     public func logout(completion: ((Result<SAResult, StreamAMGError>) -> Void)?){
-        guard let apiURL = url else {
-            let error = StreamAMGError(message: "Authentication API URL not set")
+        guard let token = lastLoginResponse?.authenticationToken else {
+            let error = StreamAMGError(message: "User is not logged in")
             completion?(.failure(error))
             return
         }
-        guard let token = lastLoginResponse?.authenticationToken else {
-            let error = StreamAMGError(message: "User is not logged in")
+        self.logoutWithToken(token: token, completion: completion)
+    }
+    
+    
+    /**
+     Logs out a user by sending a logout request with the provided token.
+
+     - Parameters:
+        - token: The authentication token for the user.
+        - completion: A closure to be called upon completion of the logout operation. It takes a `Result` enum as an argument, which can contain either a `.success` with a `SAResult` or a `.failure` with a `StreamAMGError`.
+
+     - 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.
+     */
+    public func logoutWithToken(token: String, completion: ((Result<SAResult, StreamAMGError>) -> Void)?) {
+        // Check if the API URL is set
+        guard let apiURL = url else {
+            let error = StreamAMGError(message: "Authentication API URL not set")
             completion?(.failure(error))
             return
         }
-        StreamAMGSDK.sendRequest(logoutURL(url: apiURL, token: token)){ (result: Result<LoginResponse, StreamAMGError>) in
+        
+        // Send the logout request
+        StreamAMGSDK.sendRequest(logoutURL(url: apiURL, token: token)) { (result: Result<LoginResponse, StreamAMGError>) in
             switch result {
             case .success(_):
+                // Clear the last login response and remove stored data upon successful logout
                 self.lastLoginResponse = nil
                 self.removeStoredData()
                 completion?(.success(.SALogoutOK))
             case .failure(let error):
+                // Clear the last login response and report the error in case of failure
                 self.lastLoginResponse = nil
                 completion?(.failure(error))
             }
         }
     }
+
     
     
     /// In order to perform queries against the CloudPay API a user must first initialise a session. This can be done for SSO users by generating a SSO Session.
@@ -245,21 +267,40 @@ public class AuthenticationSDK {
      * @param completion Completion block capturing StreamAMGUserModel or StreamAMGError
      */
     public func getKS(entryID: String, completion: ((Result<(SAKSResult, String), StreamAMGError>) -> Void)?){
-        guard let apiURL = url else {
-            let error = StreamAMGError(message: "Authentication API URL not set")
+        guard let token = lastLoginResponse?.authenticationToken else {
+            let error = StreamAMGError(message: "User is not logged in")
             completion?(.failure(error))
             return
         }
-        guard let token = lastLoginResponse?.authenticationToken else {
-            let error = StreamAMGError(message: "User is not logged in")
+        self.getKSWithToken(token: token, entryID: entryID, completion: completion)
+    }
+    
+    /**
+     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.
+        - completion: A closure to be called upon completion of the KS retrieval operation. It takes a `Result` enum as an argument, which can contain either a `.success` with a tuple of `SAKSResult` and the KS string or a `.failure` with a `StreamAMGError`.
+
+     - 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.
+     */
+    public func getKSWithToken(token: String, entryID: String, completion: ((Result<(SAKSResult, String), StreamAMGError>) -> Void)?) {
+        // Check if the API URL is set
+        guard let apiURL = url else {
+            let error = StreamAMGError(message: "Authentication API URL not set")
             completion?(.failure(error))
             return
         }
-        StreamAMGSDK.sendRequest(ksURL(url: apiURL, entryID: entryID, token: token)){ (result: Result<LoginResponse, StreamAMGError>) in
+        
+        // Send the KS retrieval request
+        StreamAMGSDK.sendRequest(ksURL(url: apiURL, entryID: entryID, token: token)) { (result: Result<LoginResponse, StreamAMGError>) in
             switch result {
             case .success(let data):
-                if let status = SAKSResult(rawValue: data.status ?? -2){
-                    switch status{
+                if let status = SAKSResult(rawValue: data.status ?? -2) {
+                    switch status {
                     case .Granted:
                         if let session = data.kSession {
                             completion?(.success((.Granted, session)))
@@ -287,6 +328,7 @@ public class AuthenticationSDK {
             }
         }
     }
+
     
     func securelyStoreEmailAndPass(email: String, password: String){
         _ = KeyChain.store(key: "authEmail", data: email)

From 0257d43f33d51541dc006c731c5a5fe60d0ef935 Mon Sep 17 00:00:00 2001
From: Zachariah Tom <zachariahtom@gmail.com>
Date: Tue, 10 Oct 2023 11:16:29 +0100
Subject: [PATCH 2/2] Updated read me and pod spec

---
 AuthReadme.md        | 30 ++++++++++++++++++++++++++++++
 Changelog.md         |  8 +++++---
 StreamAMGSDK.podspec |  2 +-
 3 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/AuthReadme.md b/AuthReadme.md
index 79c48f5..7d33156 100755
--- a/AuthReadme.md
+++ b/AuthReadme.md
@@ -189,6 +189,36 @@ auth.getUserSummary(token: tokens.idToken) { (result: Result<UserSummaryResponse
 }
 ```
 
+## 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.
+
+```
+auth.logoutWithToken(token: tokens.idToken) { (result: Result<SAResult, StreamAMGError>) in
+ switch result {
+  case .success(let status):
+  // Get logout status
+  case .failure(let error):
+  // Get error
+ }
+}
+```
+
+## 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.
+
+```
+auth.getKSWithToken(token: tokens.idToken, entryID: "0_validEntryID") { (result: Result<(SAKSResult, String), StreamAMGError>) in
+    switch result {
+    case .success(let response):
+        // response.1 is the valid KS
+    case .failure(let error):
+        // error includes the reason the Key Session is not provided
+    }
+}
+```
+
 Change Log:
 ===========
 
diff --git a/Changelog.md b/Changelog.md
index 0e11aa6..001aa2d 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -3,9 +3,11 @@ Change Log:
 
 All notable changes to this project will be documented in this section.
 
-### 1.2.8 - Updated MediaType APIs from POST to GET
+### 1.2.9 - Updated Authentication module with new methods to logout and getKS with third party JWT tokens.
 
-### 1.2.7 - Updated PlayKit version 
+### 1.2.8 - Updated Bitrate & mediaType APIs from POST to GET
+
+### 1.2.7 - Updated PlayKit version
 
 ### 1.2.6 - Fixed an issue with CloudMatrix response
 
@@ -24,7 +26,7 @@ All notable changes to this project will be documented in this section.
 - Default subtitle track auto-selected
 - Get Label caption on subtitle selector
 
-### 1.2.0 
+### 1.2.0
 - Updated PlayKit module.
 - Updated requirement min iOS SDK to 12+.
 - Updated AMGPurchaseDelegate to include error listener when products retrieval fails
diff --git a/StreamAMGSDK.podspec b/StreamAMGSDK.podspec
index 7db1830..81ab847 100644
--- a/StreamAMGSDK.podspec
+++ b/StreamAMGSDK.podspec
@@ -2,7 +2,7 @@
 Pod::Spec.new do |spec|
 
   spec.name         = "StreamAMGSDK"
-  spec.version      = "1.2.8"
+  spec.version      = "1.2.9"
   spec.summary      = "Stream AMG SDK"
   spec.swift_versions = "5"