Skip to content

Commit

Permalink
fix: v3 sdk bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jiananMars committed Oct 22, 2022
1 parent e50fed0 commit 5b5355f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 32 deletions.
79 changes: 59 additions & 20 deletions Authing/Authing/Network/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class AuthClient: Client {

public func registerByEmailCode(email: String, passCode: String, _ options: RegisterOptions? = nil, completion: @escaping(Response) -> Void) {
let body: NSMutableDictionary = ["connection": Connection.passcode.rawValue,
"passcodePayload": ["email": email, "passCode": passCode]]
"passCodePayload": ["email": email, "passCode": passCode]]
options?.setValues(body: body)
post("/api/v3/signup", body, completion: completion)
}
Expand All @@ -89,7 +89,7 @@ public class AuthClient: Client {
payload.setValue(code, forKey: "phoneCountryCode")
}
let body: NSMutableDictionary = ["connection": Connection.passcode.rawValue,
"passcodePayload": payload]
"passCodePayload": payload]
options?.setValues(body: body)
post("/api/v3/signup", body, completion: completion)
}
Expand All @@ -109,14 +109,18 @@ public class AuthClient: Client {
let body: NSMutableDictionary = ["connection": Connection.password.rawValue,
"passwordPayload": ["email": email, "password": encryptedPassword]]
options?.setValues(body: body)
post("/api/v3/signin", body, completion: completion)
post("/api/v3/signin", body) { res in
self.createUserInfo(nil, res: res, completion: completion)
}
}

public func loginByEmailCode(email: String, passCode: String, _ options: LoginOptions? = nil, completion: @escaping(Response) -> Void) {
let body: NSMutableDictionary = ["connection": Connection.passcode.rawValue,
"passCodePayload": ["email": email, "passCode": passCode]]
options?.setValues(body: body)
post("/api/v3/signin", body, completion: completion)
post("/api/v3/signin", body) { res in
self.createUserInfo(nil, res: res, completion: completion)
}
}

public func loginByPhoneCode(phoneCountryCode: String? = nil, phone: String, passCode: String, _ options: LoginOptions? = nil, completion: @escaping(Response) -> Void) {
Expand All @@ -127,23 +131,29 @@ public class AuthClient: Client {
let body: NSMutableDictionary = ["connection": Connection.passcode.rawValue,
"passCodePayload": payload]
options?.setValues(body: body)
post("/api/v3/signin", body, completion: completion)
post("/api/v3/signin", body) { res in
self.createUserInfo(nil, res: res, completion: completion)
}
}

public func loginByUsername(username: String, password: String, _ options: LoginOptions? = nil, completion: @escaping(Response) -> Void) {
let encryptedPassword = Util.encryptPassword(password, type: options?.passwordEncryptType)
let body: NSMutableDictionary = ["connection": Connection.password.rawValue,
"passwordPayload": ["username": username, "password": encryptedPassword]]
options?.setValues(body: body)
post("/api/v3/signin", body, completion: completion)
post("/api/v3/signin", body) { res in
self.createUserInfo(nil, res: res, completion: completion)
}
}

public func loginByAccount(account: String, password: String, _ options: LoginOptions? = nil, completion: @escaping(Response) -> Void) {
let encryptedPassword = Util.encryptPassword(password, type: options?.passwordEncryptType)
let body: NSMutableDictionary = ["connection": Connection.password.rawValue,
"passwordPayload": ["account": account, "password": encryptedPassword]]
options?.setValues(body: body)
post("/api/v3/signin", body, completion: completion)
post("/api/v3/signin", body) { res in
self.createUserInfo(nil, res: res, completion: completion)
}
}

//MARK: ---------- Social ----------
Expand Down Expand Up @@ -188,7 +198,9 @@ public class AuthClient: Client {

}

self.post("/api/v3/signin-by-mobile", body, completion: completion)
self.post("/api/v3/signin-by-mobile", body) { res in
self.createUserInfo(nil, res: res, completion: completion)
}
}
}

Expand All @@ -210,21 +222,23 @@ public class AuthClient: Client {
"yidunPayload": ["token": token,
"accessToken": accessToken]]
options?.setValues(body: body)
self.post("/api/v3/signin-by-mobile", body, completion: completion)
self.post("/api/v3/signin-by-mobile", body) { res in
self.createUserInfo(nil, res: res, completion: completion)
}
}
}

//MARK: ---------- Send SMS && Send email ----------
public func sendSms(phone: String, phoneCountryCode: String? = nil, channel: String, completion: @escaping(Response) -> Void) {
let body: NSMutableDictionary = ["phoneNumber": phone, "channel": channel]
public func sendSms(phone: String, phoneCountryCode: String? = nil, channel: Channel, completion: @escaping(Response) -> Void) {
let body: NSMutableDictionary = ["phoneNumber": phone, "channel": channel.rawValue]
if phoneCountryCode != nil {
body.setValue(phoneCountryCode, forKey: "phoneCountryCode")
}
post("/api/v3/send-sms", body, completion: completion)
}

public func sendEmail(email: String, channel: String, completion: @escaping(Response) -> Void) {
let body: NSDictionary = ["email": email, "channel": channel]
public func sendEmail(email: String, channel: Channel, completion: @escaping(Response) -> Void) {
let body: NSDictionary = ["email": email, "channel": channel.rawValue]
post("/api/v3/send-email", body, completion: completion)
}

Expand All @@ -242,14 +256,39 @@ public class AuthClient: Client {
}

//MARK: ---------- User ----------
public func getProfile(customData: Bool? = false, identities: Bool? = false, departmentIds: Bool? = false, completion: @escaping(Response) -> Void) {
get("/api/v3/get-profile?withCustomData=\(customData ?? false)&withIdentities=\(identities ?? false)&withDepartmentIds=\(departmentIds ?? false)") { res in
if res.statusCode == 200 {
let userInfo = UserInfo()
userInfo.parse(data: res.data)

// public func createUserInfo(_ code: Int, _ message: String?, _ data: NSDictionary?, completion: @escaping(Int, String?, UserInfo?) -> Void) {
// createUserInfo(nil, code, message, data, completion: completion)
// }

public func createUserInfo(_ user: UserInfo?, res: Response, completion: @escaping(Response) -> Void) {
let userInfo = user ?? UserInfo()
if res.statusCode == 200 {
userInfo.parse(data: res.data)

// only save user for 'root', otherwise we might be in console
if config == nil || config?.appId == Authing.getAppId() {
Authing.saveUser(userInfo)
} else {
ALog.i(Self.self, "requesting app ID: \(config!.appId ?? "") root app ID: \(Authing.getAppId())")
}
completion(Response(res.statusCode, res.apiCode, res.message, res.data))

completion(res)
} else if res.apiCode == EC_MFA_REQUIRED {
Authing.saveUser(userInfo)
userInfo.mfaData = res.data
completion(res)
} else if res.apiCode == EC_FIRST_TIME_LOGIN {
userInfo.firstTimeLoginToken = res.data?["token"] as? String
completion(res)
} else {
completion(res)
}
}

public func getProfile(customData: Bool? = false, identities: Bool? = false, departmentIds: Bool? = false, completion: @escaping(Response) -> Void) {
get("/api/v3/get-profile?withCustomData=\(customData ?? false)&withIdentities=\(identities ?? false)&withDepartmentIds=\(departmentIds ?? false)") { res in
self.createUserInfo(nil, res: res, completion: completion)
}
}

Expand Down Expand Up @@ -319,7 +358,7 @@ public class AuthClient: Client {
body.setValue(oldEmailPassCode, forKey: "oldEmailPassCode")
}

let dicBody: NSMutableDictionary = ["verifyMethod": "EMAIL_PASSCODE", "emailPasscodePayload": body]
let dicBody: NSMutableDictionary = ["verifyMethod": "EMAIL_PASSCODE", "emailPassCodePayload": body]

post("/api/v3/verify-update-email-request", dicBody) { res in
if res.statusCode == 200 {
Expand Down
10 changes: 3 additions & 7 deletions Authing/Authing/Network/AuthOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class AuthOptions: NSObject {

public var context: String?
public var passwordEncryptType: EncryptType? = .NONE
public var clientIp: String?
public override init() { }
}

Expand All @@ -21,7 +20,6 @@ public class LoginOptions: AuthOptions {
public var tenantId: String?
public var customData: NSDictionary?
public var autoRegister: Bool?
public var captchaCode: Bool?
public override init() { }

public func setValues(body: NSMutableDictionary){
Expand All @@ -35,9 +33,6 @@ public class LoginOptions: AuthOptions {
if let autoRegister = self.autoRegister {
optionsDic.setValue(autoRegister, forKey: "autoRegister")
}
if let captchaCode = self.captchaCode {
optionsDic.setValue(captchaCode, forKey: "captchaCode")
}
if let passwordEncryptType = self.passwordEncryptType {
optionsDic.setValue(passwordEncryptType.rawValue, forKey: "passwordEncryptType")
}
Expand Down Expand Up @@ -70,8 +65,9 @@ public class RegisterOptions: AuthOptions {
if let context = self.context {
optionsDic.setValue(context, forKey: "context")
}

body.setValue(optionsDic, forKey: "options")
if optionsDic.count != 0 {
body.setValue(optionsDic, forKey: "options")
}
if let profile = self.profile {
body.setValue(profile, forKey: "profile")
}
Expand Down
2 changes: 2 additions & 0 deletions Authing/Authing/config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
import Foundation

let SDK_VERSION: String = (Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String) ?? "1.2.0"
let EC_MFA_REQUIRED = 1636;
let EC_FIRST_TIME_LOGIN = 1639;
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@
<div align="center">
<a href="https://forum.authing.cn/" target="_blank"><img src="https://img.shields.io/badge/chat-forum-blue" /></a>
<a href="https://opensource.org/licenses/MIT" target="_blank"><img src="https://img.shields.io/badge/License-MIT-success" alt="License"></a>
<a href="javascript:;"><img src="https://img.shields.io/badge/PRs-welcome-green"></a>
<a href="https://developer.apple.com/swift/"><img src="https://img.shields.io/badge/swift-5.0-orange.svg?style=flat"></a>

<a href="https://developer.apple.com/swift/"><img src="https://img.shields.io/badge/swift-5.0-orange.svg?style=flat"></a>
<br/>
</div>

English | [简体中文](./README-zh_CN.md)

<br>

## Introduction
## Introduction

Authing - iOS SDK V3

Expand Down

0 comments on commit 5b5355f

Please sign in to comment.