Skip to content

Commit

Permalink
Add #139: Update SDK to support access token
Browse files Browse the repository at this point in the history
  • Loading branch information
Mumthasir mohammed authored and georgepadayatti committed Nov 8, 2023
1 parent a3950c1 commit c574508
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 61 deletions.
21 changes: 8 additions & 13 deletions PrivacyDashboardiOS/Classes/BBConsentPrivacyDashboardiOS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,26 @@ class BBConsentPrivacyDashboardiOS: UIViewController {

var orgId: String?
var userId: String?
var accessToken: String?
var hideBackButton = false

func log() {
debugPrint("### Log from PrivacyDashboardiOS SDK.")
}

func show(organisationId: String, apiKey: String, userId: String, animate: Bool = true) {
func show(organisationId: String, apiKey: String, userId: String, accessToken: String, animate: Bool = true) {
if #available(iOS 13.0, *) {
let appearance = UIView.appearance()
appearance.overrideUserInterfaceStyle = .light
}

orgId = organisationId
self.orgId = organisationId
self.userId = userId
if(!apiKey.isEmpty) {
let serviceManager = LoginServiceManager()
serviceManager.getUserDetails()
let data = apiKey.data(using: .utf8) ?? Data()
_ = BBConsentKeyChainUtils.save(key: "BBConsentToken", data: data)
_ = BBConsentKeyChainUtils.save(key: "BBConsentApiKey", data: data)

let frameworkBundle = Bundle(for: BBConsentOrganisationViewController.self)
let bundleURL = frameworkBundle.resourceURL?.appendingPathComponent("PrivacyDashboardiOS.bundle")
Expand Down Expand Up @@ -65,16 +66,10 @@ class BBConsentPrivacyDashboardiOS: UIViewController {
showBackButton()
}
UIApplication.topViewController()?.present(navVC, animated: true, completion: nil)
} else {
let loginVC = Constant.getStoryboard(vc: self.classForCoder).instantiateViewController(withIdentifier: "LoginVC") as! LoginViewController
let loginNav = UINavigationController.init(rootViewController: loginVC)
loginVC.orgId = organisationId
self.userId = userId
loginNav.modalPresentationStyle = .fullScreen
if !hideBackButton{
showBackButton()
}
UIApplication.topViewController()?.present(loginNav, animated: true, completion: nil)
}

if accessToken != "" {
self.accessToken = accessToken
}
}

Expand Down
6 changes: 3 additions & 3 deletions PrivacyDashboardiOS/Classes/PrivacyDashboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import Foundation

public class PrivacyDashboard {
// MARK: - Invoking 'PrivacyDashboard' iOS SDK
public static func showPrivacyDashboard(withApiKey: String, withUserId: String, withOrgId: String, withBaseUrl: String, turnOnAske: Bool, turnOnUserRequest: Bool, turnOnAttributeDetail: Bool) {
public static func showPrivacyDashboard(withApiKey: String, withUserId: String, withOrgId: String, withBaseUrl: String, accessToken: String = "", turnOnAskme: Bool, turnOnUserRequest: Bool, turnOnAttributeDetail: Bool) {
BBConsentPrivacyDashboardiOS.shared.turnOnUserRequests = turnOnUserRequest
BBConsentPrivacyDashboardiOS.shared.turnOnAskMeSection = turnOnAske
BBConsentPrivacyDashboardiOS.shared.turnOnAskMeSection = turnOnAskme
BBConsentPrivacyDashboardiOS.shared.turnOnAttributeDetailScreen = turnOnAttributeDetail
BBConsentPrivacyDashboardiOS.shared.baseUrl = withBaseUrl
BBConsentPrivacyDashboardiOS.shared.show(organisationId: withOrgId, apiKey: withApiKey, userId: withUserId)
BBConsentPrivacyDashboardiOS.shared.show(organisationId: withOrgId, apiKey: withApiKey, userId: withUserId, accessToken: accessToken)
}

// MARK: - 'Individual' related api calls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class BBConsentDashboardHeaderCell: UITableViewCell {
let modifier = AnyModifier { request in
var r = request
r.setValue("Bearer \(UserInfo.currentUser()?.token ?? "")", forHTTPHeaderField: "Authorization")
if let tokendata = BBConsentKeyChainUtils.load(key: "BBConsentToken") {
if let tokendata = BBConsentKeyChainUtils.load(key: "BBConsentApiKey") {
let token = String(data: tokendata, encoding: .utf8) ?? ""
r.setValue("ApiKey \(token)", forHTTPHeaderField: "Authorization")
r.setValue(BBConsentPrivacyDashboardiOS.shared.userId ?? "", forHTTPHeaderField: "X-ConsentBB-IndividualId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,14 @@ class BBConsentBaseWebService: NSObject {
}

func getServiceCall() {
if UserInfo.currentUser()?.token != nil{
let token : String = (UserInfo.currentUser()?.token)!
let hearDict = ["Authorization":"Bearer \(token)"]
// Note: If Apikey or UserID not available, use accessToken
if BBConsentPrivacyDashboardiOS.shared.userId == "" || BBConsentKeyChainUtils.load(key: "BBConsentApiKey") == nil {
let accessToken = BBConsentPrivacyDashboardiOS.shared.accessToken ?? ""
let hearDict = ["accesstoken":"\(accessToken)"]
header = hearDict
}else{
header = nil
}

if let tokendata = BBConsentKeyChainUtils.load(key: "BBConsentToken") {
if let tokendata = BBConsentKeyChainUtils.load(key: "BBConsentApiKey") {
let token = String(data: tokendata, encoding: .utf8) ?? ""
let hearDict = ["Authorization":"ApiKey \(token)", "X-ConsentBB-IndividualId": BBConsentPrivacyDashboardiOS.shared.userId ?? ""]
header = hearDict
Expand Down Expand Up @@ -135,15 +134,14 @@ class BBConsentBaseWebService: NSObject {


func postServiceCall() {
if UserInfo.currentUser()?.token != nil{
let token : String = (UserInfo.currentUser()?.token)!
let hearDict = ["Authorization":"Bearer \(token)"]
// Note: If Apikey or UserID not available, use accessToken
if BBConsentPrivacyDashboardiOS.shared.userId == "" || BBConsentKeyChainUtils.load(key: "BBConsentApiKey") == nil {
let accessToken = BBConsentPrivacyDashboardiOS.shared.accessToken ?? ""
let hearDict = ["accesstoken":"\(accessToken)"]
header = hearDict
}else{
header = nil
}

if let tokendata = BBConsentKeyChainUtils.load(key: "BBConsentToken") {
if let tokendata = BBConsentKeyChainUtils.load(key: "BBConsentApiKey") {
let token = String(data: tokendata, encoding: .utf8) ?? ""
let hearDict = ["Authorization":"ApiKey \(token)", "X-ConsentBB-IndividualId": BBConsentPrivacyDashboardiOS.shared.userId ?? ""]
header = hearDict
Expand Down Expand Up @@ -178,15 +176,14 @@ class BBConsentBaseWebService: NSObject {
}

func putServiceCall() {
if UserInfo.currentUser()?.token != nil{
let token : String = (UserInfo.currentUser()?.token)!
let hearDict = ["Authorization":"Bearer \(token)"]
// Note: If Apikey or UserID not available, use accessToken
if BBConsentPrivacyDashboardiOS.shared.userId == "" || BBConsentKeyChainUtils.load(key: "BBConsentApiKey") == nil {
let accessToken = BBConsentPrivacyDashboardiOS.shared.accessToken ?? ""
let hearDict = ["accesstoken":"\(accessToken)"]
header = hearDict
}else{
header = nil
}

if let tokendata = BBConsentKeyChainUtils.load(key: "BBConsentToken") {
if let tokendata = BBConsentKeyChainUtils.load(key: "BBConsentApiKey") {
let token = String(data: tokendata, encoding: .utf8) ?? ""
let hearDict = ["Authorization":"ApiKey \(token)", "X-ConsentBB-IndividualId": BBConsentPrivacyDashboardiOS.shared.userId ?? ""]
header = hearDict
Expand Down Expand Up @@ -219,14 +216,14 @@ class BBConsentBaseWebService: NSObject {


func patchServiceCall() {
if UserInfo.currentUser()?.token != nil{
let token : String = (UserInfo.currentUser()?.token)!
let hearDict = ["Authorization":"Bearer \(token)"]

// Note: If Apikey or UserID not available, use accessToken
if BBConsentPrivacyDashboardiOS.shared.userId == "" || BBConsentKeyChainUtils.load(key: "BBConsentApiKey") == nil {
let accessToken = BBConsentPrivacyDashboardiOS.shared.accessToken ?? ""
let hearDict = ["accesstoken":"\(accessToken)"]
header = hearDict
}else{
}
if let tokendata = BBConsentKeyChainUtils.load(key: "BBConsentToken") {

if let tokendata = BBConsentKeyChainUtils.load(key: "BBConsentApiKey") {
let token = String(data: tokendata, encoding: .utf8) ?? ""
let hearDict = ["Authorization":"ApiKey \(token)"]
header = hearDict
Expand Down Expand Up @@ -260,14 +257,14 @@ class BBConsentBaseWebService: NSObject {


func deleteServiceCall() {
if UserInfo.currentUser()?.token != nil{
let token : String = (UserInfo.currentUser()?.token)!
let hearDict = ["Authorization":"Bearer \(token)"]
// Note: If Apikey or UserID not available, use accessToken
if BBConsentPrivacyDashboardiOS.shared.userId == "" || BBConsentKeyChainUtils.load(key: "BBConsentApiKey") == nil {
let accessToken = BBConsentPrivacyDashboardiOS.shared.accessToken ?? ""
let hearDict = ["accesstoken":"\(accessToken)"]
header = hearDict
}else{
header = nil
}
if let tokendata = BBConsentKeyChainUtils.load(key: "BBConsentToken") {

if let tokendata = BBConsentKeyChainUtils.load(key: "BBConsentApiKey") {
let token = String(data: tokendata, encoding: .utf8) ?? ""
let hearDict = ["Authorization":"ApiKey \(token)"]
header = hearDict
Expand Down Expand Up @@ -296,14 +293,14 @@ class BBConsentBaseWebService: NSObject {
}

func upload(data: [MultipartData]) {
if UserInfo.currentUser()?.token != nil{
let token : String = (UserInfo.currentUser()?.token)!
let hearDict = ["Authorization":"Bearer \(token)"]
// Note: If Apikey or UserID not available, use accessToken
if BBConsentPrivacyDashboardiOS.shared.userId == "" || BBConsentKeyChainUtils.load(key: "BBConsentApiKey") == nil {
let accessToken = BBConsentPrivacyDashboardiOS.shared.accessToken ?? ""
let hearDict = ["accesstoken":"\(accessToken)"]
header = hearDict
}else{
header = nil
}
if let tokendata = BBConsentKeyChainUtils.load(key: "BBConsentToken") {

if let tokendata = BBConsentKeyChainUtils.load(key: "BBConsentApiKey") {
let token = String(data: tokendata, encoding: .utf8) ?? ""
let hearDict = ["Authorization":"ApiKey \(token)"]
header = hearDict
Expand Down Expand Up @@ -345,16 +342,14 @@ extension BBConsentBaseWebService {
}

func makeAPICall(urlString: String, parameters: [String: Any] = [:], headers: [String: String] = [:], method: ApiType, completion:@escaping (_ success: Bool, _ resultVal: [String: Any]) -> Void) {

if UserInfo.currentUser()?.token != nil{
let token : String = (UserInfo.currentUser()?.token)!
let hearDict = ["Authorization":"Bearer \(token)"]
// Note: If Apikey or UserID not available, use accessToken
if BBConsentPrivacyDashboardiOS.shared.userId == "" || BBConsentKeyChainUtils.load(key: "BBConsentApiKey") == nil {
let accessToken = BBConsentPrivacyDashboardiOS.shared.accessToken ?? ""
let hearDict = ["accesstoken":"\(accessToken)"]
header = hearDict
}else{
header = nil
}

if let tokendata = BBConsentKeyChainUtils.load(key: "BBConsentToken") {
if let tokendata = BBConsentKeyChainUtils.load(key: "BBConsentApiKey") {
let token = String(data: tokendata, encoding: .utf8) ?? ""
let hearDict = ["Authorization":"ApiKey \(token)", "X-ConsentBB-IndividualId": BBConsentPrivacyDashboardiOS.shared.userId ?? ""]
header = hearDict
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ class ViewController: UIViewController {
}

override func viewDidAppear(_ animated: Bool) {
PrivacyDashboard.showPrivacyDashboard(withApiKey: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJTY29wZXMiOlsic2VydmljZSJdLCJPcmdhbmlzYXRpb25JZCI6IjY1MjY1Nzk2OTM4MGYzNWZhMWMzMDI0NSIsIk9yZ2FuaXNhdGlvbkFkbWluSWQiOiI2NTI2NTc5NjkzODBmMzVmYTFjMzAyNDMiLCJleHAiOjE3MDA3MjkxOTF9.2rkHNiLDjQi8WOy4CWn96sMBx8KkvFCUMU0Xe6oXNbY", withUserId: "65378403b3f442eb9381b38d", withOrgId: "64f09f778e5f3800014a879a", withBaseUrl: "https://staging-consent-bb-api.igrant.io/v2", turnOnAske: false, turnOnUserRequest: false, turnOnAttributeDetail: false)
PrivacyDashboard.showPrivacyDashboard(withApiKey: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJTY29wZXMiOlsic2VydmljZSJdLCJPcmdhbmlzYXRpb25JZCI6IjY1MjY1Nzk2OTM4MGYzNWZhMWMzMDI0NSIsIk9yZ2FuaXNhdGlvbkFkbWluSWQiOiI2NTI2NTc5NjkzODBmMzVmYTFjMzAyNDMiLCJleHAiOjE3MDA3MjkxOTF9.2rkHNiLDjQi8WOy4CWn96sMBx8KkvFCUMU0Xe6oXNbY",
withUserId: "65378403b3f442eb9381b38d",
withOrgId: "64f09f778e5f3800014a879a",
withBaseUrl: "https://staging-consent-bb-api.igrant.io/v2",
turnOnAskme: false,
turnOnUserRequest: false,
turnOnAttributeDetail: false)
}

override func didReceiveMemoryWarning() {
Expand Down

0 comments on commit c574508

Please sign in to comment.