Sign in with Apple-蘋果登錄(客戶端和服務端)

Sign in with Apple已經很久了,之前只是看了一堆的文章理論,今天就實實在在的操作了一次,為后面項目中使用埋下基礎。這篇文章會從頭到尾描述清楚從客戶端到服務器如何一步步的實現蘋果登錄。

1.幾個官方資源

a.通過 Apple 登錄
b.Sign in with Apple REST API
c.Sign in with Apple的流程
d.從蘋果服務器驗證Apple登錄是否有效

整體的流程如下:


交互流程

2.蘋果后臺操作

  • 無論新建AppID還是老的AppID都需要配置支持Sign in with Apple


    AppID Sign in with Apple
  • 添加支持后,需要更新確認當前應用的描述文件支持Sign in with Apple


    確認描述文件
  • 項目設置支持Sign in with Apple


    項目內設置支持
  • 在Apple Developer Center添加供服務端使用的Keys


    新建Keys
  • 配置要使用Sign in with Apple的AppID


    配置Sign in with Apple
  • 生成完成后可以看到帶有Key ID(服務端要用到)的一個key,只能下載一次!!!


    生成Key
  • 下載后的p8文件,后面驗證的時候會用到


    p8文件

3.代碼開發(含服務端驗證)

a.iOS端

系統提供了ASAuthorizationAppleIDButton的按鈕可以直接使用,但也并沒有強制使用,如果用戶自定義切圖的話,和官方提供的 樣式最好保持相近。

//蘋果登錄的方法
-(void)loginWithAppleID
{
    if (@available(iOS 13.0, *)) {
        ASAuthorizationAppleIDProvider *provider = [[ASAuthorizationAppleIDProvider alloc] init];
        ASAuthorizationAppleIDRequest *request = [provider createRequest];
        request.requestedScopes = @[ASAuthorizationScopeFullName, ASAuthorizationScopeEmail];
        ASAuthorizationController *vc = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
        vc.delegate = self;
        vc.presentationContextProvider = self;
        [vc performRequests];
    } else {
        // Fallback on earlier versions
    }
}
#pragma mark - ASAuthorizationControllerPresentationContextProviding
- (ASPresentationAnchor)presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller API_AVAILABLE(ios(13.0))
{
    return self.view.window;
}
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0))
{
    if ([authorization.credential isKindOfClass:[ASAuthorizationAppleIDCredential class]])       {
        ASAuthorizationAppleIDCredential *credential = authorization.credential;
        
        NSString *state = credential.state;
        NSString *userID = credential.user;
        NSPersonNameComponents *fullName = credential.fullName;
        NSString *email = credential.email;
        NSString *authorizationCode = [[NSString alloc] initWithData:credential.authorizationCode encoding:NSUTF8StringEncoding]; // refresh token
        NSString *identityToken = [[NSString alloc] initWithData:credential.identityToken encoding:NSUTF8StringEncoding]; // access token
        ASUserDetectionStatus realUserStatus = credential.realUserStatus;
       
        NSLog(@"state: %@", state);
        NSLog(@"userID: %@", userID);
        NSLog(@"fullName: %@", fullName);
        NSLog(@"email: %@", email);
        NSLog(@"authorizationCode: %@", authorizationCode);
        NSLog(@"identityToken: %@     長度:%ld", identityToken,(long)identityToken.length);
        NSLog(@"realUserStatus: %@", @(realUserStatus));
//這里開始調用服務器的API進行登錄
        [self serververifyWithUserID:userID authorCode:authorizationCode token:identityToken];
        
    }
}
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0))
{
    NSString *errorMsg = nil;
    switch (error.code) {
        case ASAuthorizationErrorCanceled:
            errorMsg = @"用戶取消了授權請求";
            break;
        case ASAuthorizationErrorFailed:
            errorMsg = @"授權請求失敗";
            break;
        case ASAuthorizationErrorInvalidResponse:
            errorMsg = @"授權請求響應無效";
            break;
        case ASAuthorizationErrorNotHandled:
            errorMsg = @"未能處理授權請求";
            break;
        case ASAuthorizationErrorUnknown:
            errorMsg = @"授權請求失敗未知原因";
            break;
    }
    NSLog(@"%@", errorMsg);
}

在代碼中requestedScopes是用來獲取用戶信息的類型組,示例代碼中獲取了用戶的名字和郵箱(用戶可以選擇隱藏郵箱,所以拿到的郵箱不一定是真的郵箱),在獲取到用戶的信息后調用后端API驗證的時候,按照官方的描述,僅僅一個authorizationCode就可以了,在實際的開發中很多后端會讓我們把userIdidentityToken甚至BundleID也傳遞過去,方便他們的驗證。可以看出,蘋果的東西客戶端在代碼操作方面還是一如既往的方便!
由于是一個AppleID的第三方登錄,可能會存在用戶移除了授權情況,可以在應用內監聽ASAuthorizationAppleIDProviderCredentialRevokedNotification方法進行數據的對比然后做對應的處理。

b.服務端

為了方便驗證,我這里先自己作為服務器進行驗證,向https://appleid.apple.com/auth/token請求需要的幾個參數:

  • client_id:傳遞App的BundleID即可
  • code:傳遞客戶端獲取到的authorizationCode
  • grant_type:傳遞authorization_code固定字符串即可
  • client_secret:需要服務器自行計算

client_secret的計算方法:
其實是一個jwt的構建方法,下面列出一段Ruby的生成方法,讓服務器按照參數自行生成一下即可:

require "jwt"
key_file = "xxxxx.p8" #從Developer Center后臺下載的那個p8文件
team_id = "xxxxxx"             #開發者賬號的teamID
client_id = "com.xxx.xxx" #應用的BundleID
key_id = "xxxxxx"               #從Developer Center后臺找到keyid
validity_period = 180 #有效期 180天  測試的時候用 后端寫的時候 讓后端自己控制生成

private_key = OpenSSL::PKey::EC.new IO.read key_file

token = JWT.encode(
    {
        iss: team_id,
        iat: Time.now.to_i,
        exp: Time.now.to_i + 86400 * validity_period,
        aud: "https://appleid.apple.com",
        sub: client_id
    },
    private_key,
    "ES256",
    header_fields=
    {
        kid: key_id
    }
)
puts token

執行后會獲取一串字符串就是我們需要的client_secret字段。

#pragma mark - 驗證服務
-(void)serververifyWithUserID:(NSString *)uid authorCode:(NSString *)code token:(NSString *)token
{
    NSDictionary *dict1 = [self jwtDecodeWithJwtString:token];
    NSLog(@">>解析原始的:%@",dict1);
    NSDictionary *dict = @{@"client_id":@"com.sparkinglab.dsapp",@"code":code,@"grant_type":@"authorization_code",@"client_secret":@"eyJraWQiOiJURk41VTJYTks2IiwiYWxnIjoiRVMyNTYifQ.eyJpc3MiOiJLUjQzODRQV0haIiwiaWF0IjoxNTkzNDI2NzgxLCJleHAiOjE2MDg5Nzg3ODEsImF1ZCI6Imh0dHBzOi8vYXBwbGVpZC5hcHBsZS5jb20iLCJzdWIiOiJjb20uc3BhcmtpbmdsYWIuZHNhcHAifQ.PAEHDsq3tmO1bpSihnaIoAP-KOBePE7mw-U_jd6z8C1mut7jo-dyiNfnvNqzPMUXn-3pMAmoQRtj04wi632YYA"};
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager POST:@"https://appleid.apple.com/auth/token" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"--success-->%@",responseObject);
        NSDictionary *dict2 = [self jwtDecodeWithJwtString:[responseObject objectForKey:@"id_token"]];
        NSLog(@">>解析請求到的:%@",dict2);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"--error-->%@",error.localizedDescription);
    }];
}
-(NSDictionary *)jwtDecodeWithJwtString:(NSString *)jwtStr {
    NSArray * segments = [jwtStr componentsSeparatedByString:@"."];
    NSString * base64String = [segments objectAtIndex:1];
    int requiredLength = (int)(4 *ceil((float)[base64String length]/4.0));
    int nbrPaddings = requiredLength - (int)[base64String length];
    if(nbrPaddings > 0){
        NSString * pading = [[NSString string] stringByPaddingToLength:nbrPaddings withString:@"=" startingAtIndex:0];
        base64String = [base64String stringByAppendingString:pading];
    }
    base64String = [base64String stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
    base64String = [base64String stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
    NSData * decodeData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
    NSString * decodeString = [[NSString alloc] initWithData:decodeData encoding:NSUTF8StringEncoding];
    NSDictionary * jsonDict = [NSJSONSerialization JSONObjectWithData:[decodeString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
    return jsonDict;
}

客戶端拿到的identityToken其實就是一個jwt,可以直接進行解碼,就是圖中的解析原始的,可以拿到用戶的各種信息,sub就是userId,請求Apple服務器后返回的字段中id_token同樣也是一個jwt,解析后也能拿到同樣的信息,這就是為什么我在上面說給服務端一個authorizationCode就可以了,其余的信息通過Apple的服務器去驗證并獲取,基本上能請求通過就代表這用戶的真實性了,有些服務端可能會根據客戶端傳遞的userIdaud再進行一次二次比對驗證。

請求打印

以上就是完整的Sign in with Apple的實現。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。