9

如何通过 ASWebAuthenticationSession 获取身份验证 code 码

 3 years ago
source link: https://mp.weixin.qq.com/s/QUiiCKJObfDPKWCvxAg5nQ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

如何通过 ASWebAuthenticationSession 获取身份验证 code 码

Original 展菲 网罗开发 5 days ago
收录于话题
#Swift 3
#实战 35
#源码 23

1. 前言项目中需要实现 GitHub、Google、Apple 登录,实现第三方登录方案有 3 种:

  • 集成第三方一键登录
  • 分别集成 GitHub、Google、Apple 登录 SDK
  • 不集成 SDK 打开浏览器登录
今天来讲一下不集成 SDK 打开浏览器登录获取身份验证。这需要使用 ASWebAuthenticationSession 获取身份验证 code 码。网站登录身份验证逻辑:
  1. 一些网站作为一种服务提供了一种用于验证用户身份的安全机制。
  2. 当用户导航到站点的身份验证URL时,站点将向用户提供一个表单以收集凭据。
  3. 验证凭据后,站点通常使用自定义方案将用户的浏览器重定向到指示身份验证尝试结果的URL。
2. 不集成 SDK 打开浏览器登录

你可以通过使用指向身份验证网页的 URL 初始化实例来在应用程序中使用网络身份验证服务。

该页面可以是你维护的页面,也可以是由第三方操作的页面。

通过打开浏览器登录并获取身份验证 code 码,可以分为两种情况:
  1. 一种情况是在 App 内部打开浏览器获取身份验证

  2. 一种是打开手机自带浏览器获取身份验证

尝试第一种情况之后 GitHubApple 均可以正常打开浏览器并且成功登录拿到身份验证码。但是 Google 提示在 App 内部打开登录页面是不安全的,因此只能选择第二种方式。3. 打开手机自带浏览器获取身份验证

3.1 配置 URL Types

640?wx_fmt=png
建议使用 bundle id 保证唯一性。

3.2 定义全局变量

var session: ASWebAuthenticationSession!
var session : ASWebAuthenticationSession! 需要设置为全局变量,设置为局部变量会被释放掉导致弹框闪现。

3.3 获取身份验证 code 码

func oauthLogin(type: String) {    // val GitHub、Google、SignInWithApple    let redirectUrl = "配置的 URL Types"    let loginURL = Configuration.shared.awsConfiguration.authURL + "/authorize" + "?identity_provider=" + type + "&redirect_uri=" + redirectUri + "&response_type=CODE&client_id=" + Configuration.shared.awsConfiguration.appClientId    session = ASWebAuthenticationSession(url: URL(string: loginURL)!, callbackURLScheme: redirectUri) { url, error in        if error != nil {            return        }        if let responseURL = url?.absoluteString {            let components = responseURL.components(separatedBy: "#")            for item in components {                if item.contains("code") {                    let tokens = item.components(separatedBy: "&")                    for token in tokens {                        if token.contains("code") {                            let idTokenInfo = token.components(separatedBy: "=")                            if idTokenInfo.count > 1 {                                let code = idTokenInfo[1]                                print("身份验证 code 码: \(code)")                                return                            }                        }                    }                }            }        }    }    session.presentationContextProvider = self    session.start()}

这里面有两个参数,一个是 redirectUri,一个是 loginURL

redirectUri 就是 3.1 配置的白名单,作为页面重定向的唯一标示。

loginURL 是由 5 块组成:

  1. 服务器地址Configuration.shared.awsConfiguration.authURL + "/authorize"

  2. 打开的登录平台:identity_provider = "GitHub"

  3. 重定向标识:identity_provider = "配置的 URL Types"

  4. 相应类型:response_type = "CODE"

  5. 客户端 ID:client_id = "服务器配置"

回调中的 url 包含我们所需要的身份验证 code 码,需要层层解析获取 code。

3.4 指定授权界面显示的 window

告诉代理应该在哪个 window 展示授权界面给用户
#pragma mark - ASAuthorizationControllerPresentationContextProvidingextension ViewController: ASWebAuthenticationPresentationContextProviding {    func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {        return self.view.window ?? ASPresentationAnchor()    }}
获取源码方式点击左上方「网罗开发」关注并回复 “210511” 即可获取。iOS强化: 编译与链接
iOS强化 : 熟悉 Mach-O 文件iOS强化 : Xcode多环境配置CSS + HTML <文字效果>
CSS + HTML <网格布局 grid>CSS + HTML <文字排列效果>
Js <判断鼠标移入移出方向>Canvas <粒子文字效果>

领取面试题

640?wx_fmt=png

640?wx_fmt=jpeg

进入上方二维码内 回复「面试题」

如果觉得不错,素质三连、或者点个「赞」「在看」都是对笔者莫大的支持,谢谢各位大佬

Modified on 2021/05/11

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK