Skip to content

Commit 5695c76

Browse files
Merge pull request #45 from Cidaas/development
Development
2 parents 3007176 + dbe6207 commit 5695c76

File tree

11 files changed

+375
-49
lines changed

11 files changed

+375
-49
lines changed

Cidaas.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'Cidaas'
11-
s.version = '1.1.0'
11+
s.version = '1.1.0.1'
1212
s.summary = 'Native SDK for iOS providing login, registration and verification functionalities'
1313
s.homepage = 'https://github.com/Cidaas/cidaas-sdk-ios-v2'
1414
s.license = { :type => 'MIT', :file => 'LICENSE' }

Cidaas/Classes/Core/Controllers/Repository/Login/LoginController.swift

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class LoginController {
1313

1414
// shared instance
1515
public static var shared : LoginController = LoginController()
16-
public var delegate: UIViewController = UIViewController()
16+
public var delegate: UIViewController!
1717
public var storage: TransactionStore = TransactionStore.shared
1818

1919
// constructor
@@ -36,8 +36,6 @@ public class LoginController {
3636
return
3737
}
3838

39-
self.delegate = delegate
40-
4139
// construct url
4240
let loginURL = constructURL(properties: properties)
4341
let redirectURL = URL(string: properties["RedirectURL"] ?? "")!
@@ -51,25 +49,57 @@ public class LoginController {
5149
self.storage.store(session)
5250
}
5351
else {
54-
52+
self.delegate = delegate
5553
// call open safari method
5654
openSafari(loginURL : loginURL)
5755
}
56+
}
57+
58+
// login With social
59+
public func loginWithSocial(provider: String, requestId: String, delegate: UIViewController, properties: Dictionary<String, String>, callback: @escaping(Result<LoginResponseEntity>) -> Void) {
60+
// null check
61+
if properties["DomainURL"] == "" || properties["DomainURL"] == nil || properties["ClientId"] == "" || properties["ClientId"] == nil || properties["RedirectURL"] == "" || properties["RedirectURL"] == nil {
62+
let error = WebAuthError.shared.propertyMissingException()
63+
// log error
64+
let loggerMessage = "Read properties failure : " + "Error Code - " + String(describing: error.errorCode) + ", Error Message - " + error.errorMessage + ", Status Code - " + String(describing: error.statusCode)
65+
logw(loggerMessage, cname: "cidaas-sdk-error-log")
66+
67+
DispatchQueue.main.async {
68+
callback(Result.failure(error: error))
69+
}
70+
return
71+
}
5872

73+
// construct url
74+
let loginURL = constructSocialURL(provider: provider, requestId: requestId, properties: properties)
75+
let redirectURL = URL(string: properties["RedirectURL"] ?? "")!
5976

77+
if #available(iOS 11.0, *) {
78+
79+
// initiate safari session with the constructed url performing single sign on
80+
let session = SafariAuthenticationSession(loginURL: loginURL, redirectURL: redirectURL, callback: callback)
81+
82+
// save the session
83+
self.storage.store(session)
84+
}
85+
else {
86+
self.delegate = delegate
87+
// call open safari method
88+
openSafari(loginURL : loginURL)
89+
}
6090
}
6191

6292
// open safari browser. This method opens the Safari browser to display the login page. This method should be called internally and only for lower versions of ios (below 11.0)
6393
private func openSafari(loginURL : URL) {
6494

6595
// assign url to safari controller
6696
let vc = SFSafariViewController(url: loginURL)
67-
97+
6898
// present the safari controller
6999
self.delegate.present(vc, animated: true, completion: nil)
70100
}
71101

72-
private func constructURL(properties: Dictionary<String, String>) -> URL {
102+
public func constructURL(properties: Dictionary<String, String>) -> URL {
73103

74104
var urlParams = Dictionary<String, String>()
75105
urlParams["redirect_uri"] = properties["RedirectURL"] ?? ""
@@ -78,13 +108,31 @@ public class LoginController {
78108
urlParams["view_type"] = properties["ViewType"] ?? "login"
79109
urlParams["code_challenge"] = properties["Challenge"]
80110
urlParams["code_challenge_method"] = properties["Method"]
111+
urlParams["nonce"] = UUID.init().uuidString
81112

82113
var urlComponents = URLComponents(string : properties["AuthorizationURL"] ?? "")
83114
urlComponents?.queryItems = []
115+
84116
for (key, value) in urlParams {
85117
urlComponents?.queryItems?.append(URLQueryItem(name: key, value: value))
86118
}
87119

120+
for(key, value) in Cidaas.shared.extraParams {
121+
urlComponents?.queryItems?.append(URLQueryItem(name: key, value: value))
122+
}
123+
124+
return (urlComponents?.url)!
125+
}
126+
127+
public func constructSocialURL(provider: String, requestId: String, properties: Dictionary<String, String>) -> URL {
128+
129+
let baseURL = (properties["DomainURL"]) ?? ""
130+
131+
// construct url
132+
let urlString = baseURL + URLHelper.shared.getSocialLoginURL(provider: provider, requestId: requestId)
133+
134+
let urlComponents = URLComponents(string : urlString)
135+
88136
return (urlComponents?.url)!
89137
}
90138

Cidaas/Classes/Core/Helpers/General/URLHelper.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public class URLHelper {
118118
public var denyRequestURL = "/verification-srv/notification/reject"
119119
public var updateFCMTokenURL = "/devices-srv/device/updatefcm"
120120
public var pendingNotificationListURL = "/verification-srv/notification/initiated"
121+
public var socialLoginURL = "/login-srv/social/login/"
121122

122123
public func getRequestIdURL() -> String {
123124
return requestIdURL
@@ -171,6 +172,10 @@ public class URLHelper {
171172
return consentURL + "?consent_name=" + consent_name + "&version=" + String(version)
172173
}
173174

175+
public func getSocialLoginURL(provider: String, requestId: String) -> String {
176+
return socialLoginURL + provider + "/" + requestId
177+
}
178+
174179
public func getConsentDetailsURL(consent_name: String) -> String {
175180
return consentDetailsURL + "?name=" + consent_name
176181
}

Cidaas/Classes/Core/Helpers/Session/AuthSession.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ public class AuthSession: NSObject, OAuthTransactionDelegate {
2121
}
2222

2323
public func resume(_ url: URL, options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
24+
Cidaas.shared.handleToken(url: url)
2425
return true
2526
}
2627

2728
public func cancel() {
2829
// return error
30+
self.callback(Result.failure(error: WebAuthError.shared.userCancelledException()))
2931
}
3032
}

Cidaas/Classes/Core/Helpers/Session/SafariAuthenticationSession.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public class SafariAuthenticationSession : AuthSession {
2020
self.authSession = SFAuthenticationSession(url: self.loginURL, callbackURLScheme: self.redirectURL.absoluteString, completionHandler: { (resultURL, resultError) in
2121
guard resultError == nil, let callbackURL = resultURL else {
2222
if case SFAuthenticationError.canceledLogin = resultError! {
23-
// return error
23+
callback(Result.failure(error: WebAuthError.shared.userCancelledException()))
2424
} else {
25-
// return error
25+
callback(Result.failure(error: WebAuthError.shared.userCancelledException()))
2626
}
2727
return TransactionStore.shared.clear()
2828
}

Cidaas/Classes/Core/Views/Cidaas.swift

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public class Cidaas {
2525
var storage: TransactionStore
2626
var timer = Timer()
2727
var trackingManager: TrackingManager!
28-
public var browserCallback: ((Result<LoginResponseEntity>) -> ())!
28+
var browserCallback: ((Result<LoginResponseEntity>) -> ())!
29+
var propertyFileRead: Bool = false
30+
public var extraParams: Dictionary<String, String> = Dictionary<String, String>()
2931

3032
// static variables
3133
public static var intermediate_verifiation_id: String = ""
@@ -115,6 +117,7 @@ public class Cidaas {
115117
// log success
116118
let loggerMessage = "Saved Property status : \(response)"
117119
logw(loggerMessage, cname: "cidaas-sdk-success-log")
120+
self.propertyFileRead = true
118121
break
119122
}
120123
}
@@ -187,12 +190,92 @@ public class Cidaas {
187190
}
188191
}
189192

193+
// -------------------------------------------------------------------------------------------------- //
194+
195+
// login with social
196+
public func loginWithSocial(provider: String, requestId: String, delegate: UIViewController, callback: @escaping (Result<LoginResponseEntity>) -> Void) {
197+
let savedProp = DBHelper.shared.getPropertyFile()
198+
if (savedProp != nil) {
199+
self.browserCallback = callback
200+
LoginController.shared.loginWithSocial(provider: provider, requestId: requestId, delegate: delegate, properties: savedProp!, callback: callback)
201+
}
202+
else {
203+
// log error
204+
let loggerMessage = "Read properties file failure : " + "Error Code - 10001, Error Message - File not found, Status Code - 404"
205+
logw(loggerMessage, cname: "cidaas-sdk-error-log")
206+
207+
let error = WebAuthError.shared.fileNotFoundException()
208+
209+
// return failure callback
210+
DispatchQueue.main.async {
211+
callback(Result.failure(error: error))
212+
}
213+
return
214+
}
215+
}
216+
217+
// -------------------------------------------------------------------------------------------------- //
218+
219+
// get login url
220+
public func getLoginURL(callback: @escaping (Result<URL>) -> Void) {
221+
var savedProp = DBHelper.shared.getPropertyFile()
222+
if (savedProp != nil) {
223+
savedProp?["ViewType"] = "login"
224+
callback(Result.success(result: LoginController.shared.constructURL(properties: savedProp!)))
225+
}
226+
else {
227+
// log error
228+
let loggerMessage = "Read properties file failure : " + "Error Code - 10001, Error Message - File not found, Status Code - 404"
229+
logw(loggerMessage, cname: "cidaas-sdk-error-log")
230+
231+
let error = WebAuthError.shared.fileNotFoundException()
232+
233+
// return failure callback
234+
DispatchQueue.main.async {
235+
callback(Result.failure(error: error))
236+
}
237+
return
238+
}
239+
}
240+
241+
// -------------------------------------------------------------------------------------------------- //
242+
243+
// get register url
244+
public func getRegistrationURL(callback: @escaping (Result<URL>) -> Void) {
245+
var savedProp = DBHelper.shared.getPropertyFile()
246+
if (savedProp != nil) {
247+
savedProp?["ViewType"] = "register"
248+
callback(Result.success(result: LoginController.shared.constructURL(properties: savedProp!)))
249+
}
250+
else {
251+
// log error
252+
let loggerMessage = "Read properties file failure : " + "Error Code - 10001, Error Message - File not found, Status Code - 404"
253+
logw(loggerMessage, cname: "cidaas-sdk-error-log")
254+
255+
let error = WebAuthError.shared.fileNotFoundException()
256+
257+
// return failure callback
258+
DispatchQueue.main.async {
259+
callback(Result.failure(error: error))
260+
}
261+
return
262+
}
263+
}
264+
190265
// -------------------------------------------------------------------------------------------------- //
191266

192267
// handle token
193268
public func handleToken(url: URL) {
194-
let code = url.valueOf("code") ?? ""
195-
AccessTokenController.shared.getAccessToken(code: code, callback: browserCallback!)
269+
if LoginController.shared.delegate != nil {
270+
LoginController.shared.delegate.dismiss(animated: true, completion: nil)
271+
}
272+
273+
if browserCallback != nil {
274+
let code = url.valueOf("code") ?? ""
275+
if code != "" {
276+
AccessTokenController.shared.getAccessToken(code: code, callback: browserCallback!)
277+
}
278+
}
196279
}
197280

198281
// -------------------------------------------------------------------------------------------------- //
@@ -2535,7 +2618,7 @@ public class Cidaas {
25352618
// 2. Call getPendingNotifications method
25362619
// 3. Maintain logs based on flags
25372620

2538-
public func getPendingNotifications(sub: String, fcmId: String, callback: @escaping(Result<PendingNotificationListResponseEntity>) -> Void) {
2621+
public func getPendingNotifications(sub: String, callback: @escaping(Result<PendingNotificationListResponseEntity>) -> Void) {
25392622

25402623
let savedProp = DBHelper.shared.getPropertyFile()
25412624
if (savedProp != nil) {

Example/Cidaas/AppDelegate.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1616

1717
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1818
// Override point for customization after application launch.
19-
// Cidaas.shared.ENABLE_LOG = true
19+
Cidaas.shared.ENABLE_LOG = true
2020
return true
2121
}
2222

@@ -31,6 +31,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
3131
return true
3232
}
3333

34+
3435

3536
func applicationWillResignActive(_ application: UIApplication) {
3637
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

Example/Cidaas/Info.plist

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@
2323
<key>CFBundleURLTypes</key>
2424
<array>
2525
<dict>
26+
<key>CFBundleTypeRole</key>
27+
<string>Editor</string>
28+
<key>CFBundleURLName</key>
29+
<string>com.cidaas.sdk.demo</string>
2630
<key>CFBundleURLSchemes</key>
2731
<array>
28-
<string>fb1889502804712451</string>
32+
<string>com.cidaas.sdk.demo</string>
2933
</array>
3034
</dict>
3135
</array>
@@ -52,7 +56,7 @@
5256
<string>Location in foreground</string>
5357
<key>UIBackgroundModes</key>
5458
<array>
55-
<string>remote-notification</string>
59+
<string>fetch</string>
5660
</array>
5761
<key>UILaunchStoryboardName</key>
5862
<string>LaunchScreen</string>

Example/Cidaas/ViewController.swift

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ import Cidaas
1111

1212
class ViewController: UIViewController {
1313

14+
var requestId: String = ""
1415

1516
// did load
1617
override func viewDidLoad() {
1718
super.viewDidLoad()
18-
19+
var dict = Dictionary<String, String>()
20+
dict["scope"] = "openid email profile offline_access"
21+
Cidaas.shared.extraParams = dict
1922
}
2023

2124
// did receive memory warning
@@ -24,17 +27,46 @@ class ViewController: UIViewController {
2427
}
2528

2629
@IBAction func loginAction(_ sender: Any) {
27-
Cidaas.shared.loginWithBrowser(delegate: self) {
30+
31+
Cidaas.shared.getRequestId() {
2832
switch $0 {
29-
case .failure(let errorResponse):
30-
print(errorResponse.errorMessage)
31-
break
32-
case .success(let successResponse):
33-
print(successResponse.data.access_token)
34-
break
33+
case .success(let resultURL):
34+
print(resultURL.data.requestId)
35+
self.requestId = resultURL.data.requestId
36+
37+
Cidaas.shared.loginWithSocial(provider: "facebook", requestId: self.requestId, delegate: self) {
38+
switch $0 {
39+
case .success(let loginSuccessResponse):
40+
print(loginSuccessResponse.data.access_token)
41+
break
42+
case .failure(let loginErrorResponse):
43+
print(loginErrorResponse.errorMessage)
44+
break
45+
}
46+
}
47+
48+
break
49+
case .failure(let errorResponse):
50+
print(errorResponse.errorMessage)
51+
break
3552
}
3653
}
3754

55+
// Cidaas.shared.loginWithBrowser(delegate: self) {
56+
// switch $0 {
57+
// case .failure(let errorResponse):
58+
// print(errorResponse.errorMessage)
59+
// break
60+
// case .success(let successResponse):
61+
// let alert = UIAlertController(title: "Access Token", message: "\(successResponse.data.access_token)", preferredStyle: .alert)
62+
// alert.addAction(UIAlertAction(title: "Done", style: .default, handler: nil))
63+
// self.present(alert, animated: true, completion: nil)
64+
// break
65+
// }
66+
// }
67+
68+
// guard let url = URL(string: "https://stackoverflow.com") else { return }
69+
// UIApplication.shared.open(url)
70+
3871
}
3972
}
40-

0 commit comments

Comments
 (0)