我正在研究 Auth0 集成,我在我的 Swift 项目中成功集成了 Auth0 SDK,但我想从我的应用程序实现直接登录,而不重定向到 Auth0 通用登录页面。
我在这里研究了移动登录流程(https://auth0.com/docs/flows/concepts/mobile-login-flow )。
我在 iOS Swift 中实现了 Auth0 登录,它正在工作。但我想直接登录。
查看我的屏幕
单击继续它将打开 Auth0.com 页面(我不想要这个页面,我想直接登录没有这个页面如何?)
我不想要这个页面,我想通过mu app登录页面直接登录没有这个页面,如何?。
可能吗?。
为此,我点击了此链接https://auth0.com/docs/flows/guides/mobile-login-flow/add-login-using-mobile-login-flow并实施了 code_verifier 和 code_challage。但是当我实现Authorize the User时,它会给出html response。
我的代码是:
func codeVerifier() {
var buffer = [UInt8](repeating: 0, count: 32)
_ = SecRandomCopyBytes(kSecRandomDefault, buffer.count, &buffer)
let verifier = Data(bytes: buffer).base64EncodedString()
.replacingOccurrences(of: "+", with: "-")
.replacingOccurrences(of: "/", with: "$_")
.replacingOccurrences(of: "=", with: "")
.trimmingCharacters(in: .whitespaces)
print("Code_Verifier : \(verifier)")
codeChallenger(verifier: verifier)
}
func codeChallenger(verifier:String) {
// Dependency: Apple Common Crypto library
// http://opensource.apple.com//source/CommonCrypto
guard let data = verifier.data(using: .utf8) else {
return
}
var buffer = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA256($0, CC_LONG(data.count), &buffer)
}
let hash = Data(bytes: buffer)
let challenge = hash.base64EncodedString()
.replacingOccurrences(of: "+", with: "-")
.replacingOccurrences(of: "/", with: "$_")
.replacingOccurrences(of: "=", with: "")
.trimmingCharacters(in: .whitespaces)
print("Code_Challenger : \(challenge)")
authorizwTheUser(code_challange: challenge)
}
func authorizwTheUser(code_challange:String) {
let url = "https://domain.auth0.com/authorize?"
var request = URLRequest(url: URL(string: url)!)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "GET"
print("URL : \(request)")
let parameters = "response_type=token&code_challenge=\(code_challange)&code_challenge_method=S256&client_id=&redirect_uri=com.myappname.Auth0DemoSwift://domainname.auth0.com/ios/com.domainname.Auth0DemoSwift/callback&scope=openid profile&state=xyzABC123x"
request.httpBody = parameters.data(using: .utf8)
print(parameters)
let task = URLSession.shared.dataTask(with: request) { data, response, error in guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
// If Response is in String formate
let responseString = String(data: data, encoding: .utf8)
let dictionary = data
print("dictionary = \(dictionary)")
print("responseString = \(String(describing: responseString!))")
do {
let response = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: AnyObject]
print(response!)
let res = response!["Response"]
let status = res!["status"] as! String
if status == "SUCCESS" {
} else {
}
} catch let error as NSError {
print(error)
}
}
task.resume()
}