[ 01 . Apple LogIn의 Init ]
[ 초기화 ]
using AppleAuth;
using AppleAuth.Enums;
using AppleAuth.Extensions;
using AppleAuth.Interfaces;
using AppleAuth.Native;
//Auth를 담당하는 매니저
private IAppleAuthManager appleAuthManager;
void Start()
{
// If the current platform is supported : 현재 플랫폼에서 지원한다면
if (AppleAuthManager.IsCurrentPlatformSupported)
{
// Creates a default JSON deserializer, to transform JSON Native responses to C# instances
var deserializer = new PayloadDeserializer();
// Creates an Apple Authentication manager with the deserializer
this.appleAuthManager = new AppleAuthManager(deserializer);
}
}
void Update()
{
// Updates the AppleAuthManager instance to execute
// pending callbacks inside Unity's execution loop
if (this.appleAuthManager != null)
{
this.appleAuthManager.Update();
}
}
=>처음에 AppleAuth 관련한 초기화를 진행한다 .
[ 02 . Apple LogIn ]
[ 01 . Step 2: Generating a random RAW Nonce ]
Firebase로 보낼 임의의 문자열을 생성하고 , Apple에서 받은 자격 증명이 올바른지 확인해야 한다.
이를 위해 랜덤으로 문자열을 생성하는 메서드를 사용한다.
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
private static string GenerateRandomString(int length)
{
if (length <= 0)
{
throw new Exception("Expected nonce to have positive length");
}
const string charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._";
var cryptographicallySecureRandomNumberGenerator = new RNGCryptoServiceProvider();
var result = string.Empty;
var remainingLength = length;
var randomNumberHolder = new byte[1];
while (remainingLength > 0)
{
var randomNumbers = new List<int>(16);
for (var randomNumberCount = 0; randomNumberCount < 16; randomNumberCount++)
{
cryptographicallySecureRandomNumberGenerator.GetBytes(randomNumberHolder);
randomNumbers.Add(randomNumberHolder[0]);
}
for (var randomNumberIndex = 0; randomNumberIndex < randomNumbers.Count; randomNumberIndex++)
{
if (remainingLength == 0)
{
break;
}
var randomNumber = randomNumbers[randomNumberIndex];
if (randomNumber < charset.Length)
{
result += charset[randomNumber];
remainingLength--;
}
}
}
return result;
}
[ 02 . Step 2: Generate the SHA256 of the RAW Nonce ]
Apple 로그인시 사용하기 위해 보낼 nonce 를SHA256해시로 만들어서 전달해야 한다 .
using System.Security.Cryptography;
using System.Text;
private static string GenerateSHA256NonceFromRawNonce(string rawNonce)
{
var sha = new SHA256Managed();
var utf8RawNonce = Encoding.UTF8.GetBytes(rawNonce);
var hash = sha.ComputeHash(utf8RawNonce);
var result = string.Empty;
for (var i = 0; i < hash.Length; i++)
{
result += hash[i].ToString("x2");
}
return result;
}
[ 03. 생성된 정보를 바탕으로 Apple Login ]
Apple 로그인을 시도하고 , 성공시 Firebase에 인증을 시도한다 .
이때 , 인자로 받는 firebaseAuthCallback은 성공시 불려질 콜백이다 .
public void PerformLoginWithAppleIdAndFirebase(Action<FirebaseUser> firebaseAuthCallback)
{
var rawNonce = GenerateRandomString(32);
var nonce = GenerateSHA256NonceFromRawNonce(rawNonce);
var loginArgs = new AppleAuthLoginArgs(
LoginOptions.IncludeEmail | LoginOptions.IncludeFullName,
nonce);
this.appleAuthManager.LoginWithAppleId(
loginArgs,
credential =>
{
var appleIdCredential = credential as IAppleIDCredential;
if (appleIdCredential != null)
{
this.PerformFirebaseAuthentication(appleIdCredential, rawNonce, firebaseAuthCallback);
}
},
error =>
{
// Something went wrong
});
}
[ 04. Firebase에서 인증하기 ]
Apple 로그인 정보를 바탕으로 Firebase에 인증한다 .
using System;
using System.Text;
using AppleAuth.Interfaces;
using Firebase.Auth;
using Firebase.Extensions;
using UnityEngine;
// Your Firebase authentication client
private FirebaseAuth firebaseAuth;
private void PerformFirebaseAuthentication(
IAppleIDCredential appleIdCredential,
string rawNonce,
Action<FirebaseUser> firebaseAuthCallback)
{
var identityToken = Encoding.UTF8.GetString(appleIdCredential.IdentityToken);
var authorizationCode = Encoding.UTF8.GetString(appleIdCredential.AuthorizationCode);
var firebaseCredential = OAuthProvider.GetCredential(
"apple.com",
identityToken,
rawNonce,
authorizationCode);
this.firebaseAuth.SignInWithCredentialAsync(firebaseCredential)
.ContinueWithOnMainThread(task => HandleSignInWithUser(task, firebaseAuthCallback));
}
private static void HandleSignInWithUser(Task<FirebaseUser> task, Action<FirebaseUser> firebaseUserCallback)
{
if (task.IsCanceled)
{
Debug.Log("Firebase auth was canceled");
firebaseUserCallback(null);
}
else if (task.IsFaulted)
{
Debug.Log("Firebase auth failed");
firebaseUserCallback(null);
}
else
{
var firebaseUser = task.Result;
Debug.Log("Firebase auth completed | User ID:" + firebaseUser.UserId);
firebaseUserCallback(firebaseUser);
}
}
출처
Sign in with Apple Unity Plugin: Working with Firebase
Unity plugin to support Sign In With Apple Id. Contribute to lupidan/apple-signin-unity development by creating an account on GitHub.
github.com
http://milennium9.godohosting.com/wordpress/?p=300
[Unity] Apple Login과 Firebase 인증
Apple이 익명 로그인 기능만 사용할게 아니라면 Apple Login을 의무화 하였기 때문에 Apple Login을 지원하지 않으면 리젝을 당하게 됩니다. 그래서 Apple Login을 프로젝트에 추가하는 과정을 간략하게 남
milennium9.godohosting.com
http://milennium9.godohosting.com/wordpress/?p=300
[Unity] Apple Login과 Firebase 인증
Apple이 익명 로그인 기능만 사용할게 아니라면 Apple Login을 의무화 하였기 때문에 Apple Login을 지원하지 않으면 리젝을 당하게 됩니다. 그래서 Apple Login을 프로젝트에 추가하는 과정을 간략하게 남
milennium9.godohosting.com
'유니티 > Firebase' 카테고리의 다른 글
[ Firebase ] IOS 인증 구현 01 . 세팅하기 (0) | 2023.07.11 |
---|---|
[ Firebase ] Storage (0) | 2023.06.18 |
[ Firebase ] 02 . 회원가입과 로그인 구현하기 (0) | 2023.06.17 |
[ Firebase ] 01 . 기초 알아보기 (0) | 2023.06.16 |