Unity 接入Apple登录

        文章末尾有资源插件链接

在6月的全球开发者大会(WWDC)上,苹果宣布了一款新产品:使用苹果登录。随着iOS 13即将于9月19日发布,苹果已经更新了App Store审查指南,现在他们要求任何使用第三方或社交登录服务的新应用程序提供与苹果相同的登录选项。现有的申请将被要求在2020年4月之前完成。你可以在苹果的开发者网站上阅读更多关于这一变化的信息。

我们知道许多Unity开发者依赖于第三方登录服务。为了更容易遵守这些新准则,我们创建了一个新的资产存储包。您可以将该软件包添加到新的或现有的项目中,以轻松利用Apple的新登录功能。

下面你会发现以下内容:

  1. 如何使用新的资产存储插件的一步一步指南
  2. 重要准则的链接
  3. 对于更高级的用例,概述如何执行服务器端验证

入门

为了开始用苹果登录,我们创建了一个新的资产商店包,这个包的目的是使新添加的iOS13 API可用,以使用苹果登录。

你还需要Xcode11,它可以在macOS 10.14(Mojave)或10.15(Catalina)上工作,以及安装了iOS13.0的设备。Xcode和iOS13的下载可以在这里找到。此外,还建议您阅读并审阅登录Apple入门指南。本指南涵盖了苹果的人机界面指南和应用商店审查指南。您的应用程序还需要在Apple的开发人员门户中启用Apple登录功能。进一步的说明可以在这里找到。

您可以从Unity Asset Store下载新包。若要使用包:

  1. 将包导入到现有的Unity项目中。

                Unity 接入Apple登录_第1张图片

  1. 创建一个脚本,它有一个回调函数来接收苹果的登录数据。 请参阅示例脚本“SignInWithApple.cs”,以了解如何使用API.
  2. 重建你的Unity项目,打开由Unity创建的Xcode项目.
  3. 授权和框架依赖需要在Xcode中配置.
    1. 在目标设置中必须添加“用苹果登录”功能。 点击“+ Capability”按钮,选择“Sign in with Apple”,如下图所示
                         Unity 接入Apple登录_第2张图片
 
    1. 要将框架标记为可选的,选择Build Phases标签,展开Link Binary with Libraries部分。 找到AuthenticationServices框架,将Status从“Required”更改为“Optional”。

                        Unity 接入Apple登录_第3张图片

    1. 要将框架标记为可选的,选择Build Phases标签,展开Link Binary with Libraries部分。 找到AuthenticationServices框架,将Status从“Required”更改为“Optional”

                         Unity 接入Apple登录_第4张图片.

注意:使用Unity 2019.2或更低版本创建的Xcode项目将不会有UnityFramework目标。 AuthenticationServices框架应该被添加到Unity-iPhone目标中.

  1. 配置好项目后,您现在可以提取回调中提供的任何数据,以便与项目现有代码库一起使用  .

服务器端验证

对于需要服务器端身份验证的游戏或应用程序,您可以将identityToken传递给服务器进行验证.

苹果的identityToken是客户端无法生成的JWT令牌。 为了验证JWT令牌是由苹果发布的,并打算由你的应用程序使用,你必须验证:  

  • 应用标准的JWT验证:
    • 验证token的格式是否合法.
    • 验证令牌签名是否有效。 有效的密钥可以在JWKS URL: https://appleid.apple.com/auth/keys中找到 
    • 确认iss为https://appleid.apple.com
    • 验证token是否过期.
      • 确保exp声明必须在当前时间之后 .
  • 检查JWT令牌的受众是否适合你的应用.
    • 你从Sign in with Apple获得的令牌使用你的iOS应用ID作为受众。 您的服务器端需要验证它,以确保它适用于您的应用程序.

苹果JWT有效载荷声称:

Claim

Column Name

Type

Example

Description

iss

Issuer

string

https://app leid.apple. com

The value is always https://appleid.apple.com

aud

Audience

string

com.unity. testApp

The audience of the token.

exp

Expiration Time

number

15686716

00

The expiration time in epoch (seconds since 1970-01-01 00:00:00Z) of the token.

iat

Issued At

number

15686710

00

The time in epoch at which the token is issued.

sub

Subject

string

001999.8

0b18c74c 3264cad8 95d0eae1 81d8f50.1

909

The user ID of the authenticated user.

c_hash

Code Hash

string

agyAh42 GdE-O72 Y4HUHyp

g

The hash of the authorization code. It's only used when you need to validate the authorization code.

email

Email

string

xxx@priva terelay.ap pleid.com

The email address of the user.

email_verified

Email Verified

string

true

Whether the email is verified. Note that it's a string JSON type.

auth_time

Auth Time

number

15686710

00

The time in epoch at which the authentication happened.

在大多数编程语言中都有JWT库。 这些库可以帮助您 parse and validate the token(解析和验证令牌 ).

如果使用javascript,下面是一个如何验证令牌的示例。它从stdin读取令牌,并尝试验证令牌。将“your.app.id”替换为来自苹果的真实应用id.

const jwt = require('jsonwebtoken') const jwksClient = require('jwks-rsa'); fs = require('fs');

var token = fs.readFileSync('/dev/stdin').toString().trim(); console.log(token);

var client = jwksClient({
jwksUri: 'https://appleid.apple.com/auth/keys'
});


function getApplePublicKey(header, callback) { client.getSigningKey(header.kid, function (err, key) {
var signingKey = key.publicKey || key.rsaPublicKey; callback(null, signingKey);
});
}


jwt.verify(token, getApplePublicKey, null, function (err, decoded) { if (err) {
console.error(err); process.exit(1);
}
if (decoded.iss !== "https://appleid.apple.com") { console.error("unexpected issuer (iss claim): ", decoded.iss); process.exit(1);
}
if (decoded.aud !== "your.app.id") {
console.error("unexpected audience (aud claim): ", decoded.aud); process.exit(1);
}
console.log("Validated Apple token: ", decoded);
});

资源链接 Sign-in-with-Apple Demo下载

你可能感兴趣的:(Unity,笔记,IOS,unity)