Skip to content

Commit

Permalink
Merge pull request #218 from lupidan/canary
Browse files Browse the repository at this point in the history
 v1.4.4 Release
  • Loading branch information
lupidan authored Nov 3, 2024
2 parents 4731e04 + baee18b commit 87d01af
Show file tree
Hide file tree
Showing 30 changed files with 797 additions and 186 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/build-and-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ on: workflow_dispatch
jobs:
Build-And-Check:
runs-on: ubuntu-latest
strategy:
matrix:
unity-version:
- "6000.0.23f1"
- "2022.3.51f1"
- "2021.3.45f1"
- "2020.3.48f1"

steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand All @@ -18,11 +26,11 @@ jobs:
with:
projectPath: ./AppleAuthSampleProject
targetPlatform: iOS
unityVersion: 6000.0.23f1
unityVersion: ${{ matrix.unity-version }}

- name: Upload Xcode artifact
uses: actions/upload-artifact@v4
with:
name: 6000_0_3f1_XcodeBuild
name: "${{ matrix.unity-version }}_XcodeBuild"
path: build

6 changes: 3 additions & 3 deletions AppleAuth/AppleAuthManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if ((UNITY_IOS || UNITY_TVOS || UNITY_STANDALONE_OSX) && !UNITY_EDITOR)
#if ((UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS || UNITY_STANDALONE_OSX) && !UNITY_EDITOR)
#define APPLE_AUTH_MANAGER_NATIVE_IMPLEMENTATION_AVAILABLE
#endif

Expand All @@ -12,7 +12,7 @@ public class AppleAuthManager : IAppleAuthManager
{
static AppleAuthManager()
{
const string versionMessage = "Using Sign in with Apple Unity Plugin - v1.4.3";
const string versionMessage = "Using Sign in with Apple Unity Plugin - v1.4.4";
#if APPLE_AUTH_MANAGER_NATIVE_IMPLEMENTATION_AVAILABLE
PInvoke.AppleAuth_LogMessage(versionMessage);
#else
Expand Down Expand Up @@ -289,7 +289,7 @@ public Entry(bool isSingleUseCallback, Action<string> messageCallback)

private static class PInvoke
{
#if UNITY_IOS || UNITY_TVOS
#if UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS
private const string DllName = "__Internal";
#elif UNITY_STANDALONE_OSX
private const string DllName = "MacOSAppleAuthManager";
Expand Down
102 changes: 57 additions & 45 deletions AppleAuth/Editor/ProjectCapabilityManagerExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if UNITY_IOS || UNITY_TVOS
#if UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS

using System;
using System.Reflection;
Expand All @@ -8,69 +8,81 @@ namespace AppleAuth.Editor
{
public static class ProjectCapabilityManagerExtension
{
private const string EntitlementsArrayKey = "com.apple.developer.applesignin";
private const string DefaultAccessLevel = "Default";
private const string AuthenticationServicesFramework = "AuthenticationServices.framework";
private const BindingFlags NonPublicInstanceBinding = BindingFlags.NonPublic | BindingFlags.Instance;
private const BindingFlags PublicInstanceBinding = BindingFlags.Public | BindingFlags.Instance;

/// <summary>
/// Extension method for ProjectCapabilityManager to add the Sign In With Apple capability in compatibility mode.
/// In particular, adds the AuthenticationServices.framework as an Optional framework, preventing crashes in
/// iOS versions previous to 13.0
/// </summary>
/// <param name="manager">The manager for the main target to use when adding the Sign In With Apple capability.</param>
/// <param name="unityFrameworkTargetGuid">The GUID for the UnityFramework target. If null, it will use the main target GUID.</param>
public static void AddSignInWithAppleWithCompatibility(this ProjectCapabilityManager manager, string unityFrameworkTargetGuid = null)
public static void AddSignInWithAppleWithCompatibility(this ProjectCapabilityManager manager)
{
const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
var managerType = typeof(ProjectCapabilityManager);

var projectField = managerType.GetField("project", NonPublicInstanceBinding);
var targetGuidField = managerType.GetField("m_TargetGuid", NonPublicInstanceBinding);
var entitlementFilePathField = managerType.GetField("m_EntitlementFilePath", NonPublicInstanceBinding);
var getOrCreateEntitlementDocMethod = managerType.GetMethod("GetOrCreateEntitlementDoc", NonPublicInstanceBinding);

// in old unity versions PBXCapabilityType had internal ctor; that was changed to public afterwards - try both
var constructorInfo = GetPBXCapabilityTypeConstructor(PublicInstanceBinding) ??
GetPBXCapabilityTypeConstructor(NonPublicInstanceBinding);

if (projectField == null || targetGuidField == null || entitlementFilePathField == null ||
getOrCreateEntitlementDocMethod == null || constructorInfo == null)
throw new Exception("Can't Add Sign In With Apple programatically in this Unity version");
var projectField = managerType.GetField("project", bindingFlags);
var entitlementFilePathField = managerType.GetField("m_EntitlementFilePath", bindingFlags);
var targetGuidField = managerType.GetField("m_TargetGuid", bindingFlags);
var getOrCreateEntitlementDocMethod = managerType.GetMethod("GetOrCreateEntitlementDoc", bindingFlags);
if (projectField == null ||
entitlementFilePathField == null ||
targetGuidField == null ||
getOrCreateEntitlementDocMethod == null)
throw new Exception("Can't Add Sign In With Apple programatically in this Unity version.");

var entitlementFilePath = entitlementFilePathField.GetValue(manager) as string;
var entitlementDoc = getOrCreateEntitlementDocMethod.Invoke(manager, new object[] { }) as PlistDocument;
var entitlementDoc = (PlistDocument) getOrCreateEntitlementDocMethod.Invoke(manager, new object[] { });
if (entitlementDoc != null)
{
var plistArray = new PlistElementArray();
plistArray.AddString(DefaultAccessLevel);
entitlementDoc.root[EntitlementsArrayKey] = plistArray;
plistArray.AddString("Default");
entitlementDoc.root["com.apple.developer.applesignin"] = plistArray;
}

var project = projectField.GetValue(manager) as PBXProject;
if (project != null)
{
var mainTargetGuid = targetGuidField.GetValue(manager) as string;
var capabilityType = constructorInfo.Invoke(new object[] { "com.apple.developer.applesignin.custom", true, string.Empty, true }) as PBXCapabilityType;

var targetGuidToAddFramework = unityFrameworkTargetGuid;
if (targetGuidToAddFramework == null)
{
targetGuidToAddFramework = mainTargetGuid;
}

project.AddFrameworkToProject(targetGuidToAddFramework, AuthenticationServicesFramework, true);
project.AddCapability(mainTargetGuid, capabilityType, entitlementFilePath, false);
}
var project = (PBXProject) projectField.GetValue(manager);
var emptyCapability = GetEmptyCapabilityWithReflection();

var mainTargetGuid = (string)targetGuidField.GetValue(manager);
#if UNITY_2019_3_OR_NEWER
var frameworkTargetGuid = project.GetUnityFrameworkTargetGuid();
#else
var frameworkTargetGuid = mainTargetGuid;
#endif

project.AddFrameworkToProject(frameworkTargetGuid, "AuthenticationServices.framework", true);
project.AddCapability(mainTargetGuid, emptyCapability, entitlementFilePath);
}

private static ConstructorInfo GetPBXCapabilityTypeConstructor(BindingFlags flags)
private static PBXCapabilityType GetEmptyCapabilityWithReflection()
{
return typeof(PBXCapabilityType).GetConstructor(
flags,
null,
new[] {typeof(string), typeof(bool), typeof(string), typeof(bool)},
null);
// For Unity version >= 6000.0.23f1
var constructorInfo = typeof(PBXCapabilityType)
.GetConstructor(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
null,
new[] {typeof(bool), typeof(string), typeof(bool)},
null);

if (constructorInfo != null)
{
return (PBXCapabilityType) constructorInfo
.Invoke(new object[] {true, string.Empty, true});
}

// For Unity version < 6000.0.23f1
constructorInfo = typeof(PBXCapabilityType)
.GetConstructor(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
null,
new[] {typeof(string), typeof(bool), typeof(string), typeof(bool)},
null);

if (constructorInfo != null)
{
return (PBXCapabilityType) constructorInfo
.Invoke(new object[] {"com.lupidan.apple-signin-unity.empty", true, string.Empty, true});
}

throw new Exception("Can't create empty capability in this Unity version.");
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions AppleAuth/Extensions/PersonNameExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if ((UNITY_IOS || UNITY_TVOS || UNITY_STANDALONE_OSX) && !UNITY_EDITOR)
#if ((UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS || UNITY_STANDALONE_OSX) && !UNITY_EDITOR)
#define NATIVE_PERSON_NAME_COMPONENTS_AVAILABLE
#endif

Expand Down Expand Up @@ -77,7 +77,7 @@ private static void TryAddKeyValue(string format, string key, string value, Syst

private static class PInvoke
{
#if UNITY_IOS || UNITY_TVOS
#if UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS
private const string DllName = "__Internal";
#elif UNITY_STANDALONE_OSX
private const string DllName = "MacOSAppleAuthManager";
Expand Down
22 changes: 4 additions & 18 deletions AppleAuth/Native/iOS/AppleAuthManager.h.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 16 additions & 15 deletions AppleAuth/Native/iOS/AppleAuthManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

#pragma mark - AppleAuthManager Implementation

// IOS/TVOS 13.0 | MACOS 10.15
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 || __TV_OS_VERSION_MAX_ALLOWED >= 130000 || __MAC_OS_X_VERSION_MAX_ALLOWED >= 101500
// IOS/TVOS 13.0 | MACOS 10.15 | VISIONOS 1.0
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 || __TV_OS_VERSION_MAX_ALLOWED >= 130000 || __MAC_OS_X_VERSION_MAX_ALLOWED >= 101500 || __VISION_OS_VERSION_MAX_ALLOWED >= 10000
#define AUTHENTICATION_SERVICES_AVAILABLE true
#import <AuthenticationServices/AuthenticationServices.h>
#endif
Expand All @@ -45,7 +45,7 @@ - (void) sendsLoginResponseInternalErrorWithCode:(NSInteger)code andMessage:(NSS
@end

#if AUTHENTICATION_SERVICES_AVAILABLE
API_AVAILABLE(ios(13.0), macos(10.15), tvos(13.0), watchos(6.0))
API_AVAILABLE(ios(13.0), macos(10.15), tvos(13.0), watchos(6.0), visionos(1.0))
@interface AppleAuthManager () <ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding>
@property (nonatomic, strong) ASAuthorizationAppleIDProvider *appleIdProvider;
@property (nonatomic, strong) ASAuthorizationPasswordProvider *passwordProvider;
Expand Down Expand Up @@ -74,7 +74,7 @@ - (instancetype) init
if (self)
{
#if AUTHENTICATION_SERVICES_AVAILABLE
if (@available(iOS 13.0, tvOS 13.0, macOS 10.15, *))
if (@available(iOS 13.0, tvOS 13.0, macOS 10.15, visionOS 1.0, *))
{
_appleIdProvider = [[ASAuthorizationAppleIDProvider alloc] init];
_passwordProvider = [[ASAuthorizationPasswordProvider alloc] init];
Expand All @@ -90,7 +90,7 @@ - (instancetype) init
- (void) quickLogin:(uint)requestId withNonce:(NSString *)nonce andState:(NSString *)state
{
#if AUTHENTICATION_SERVICES_AVAILABLE
if (@available(iOS 13.0, tvOS 13.0, macOS 10.15, *))
if (@available(iOS 13.0, tvOS 13.0, macOS 10.15, visionOS 1.0, *))
{
ASAuthorizationAppleIDRequest *appleIDRequest = [[self appleIdProvider] createRequest];
[appleIDRequest setNonce:nonce];
Expand All @@ -117,7 +117,7 @@ - (void) quickLogin:(uint)requestId withNonce:(NSString *)nonce andState:(NSStri
- (void) loginWithAppleId:(uint)requestId withOptions:(AppleAuthManagerLoginOptions)options nonce:(NSString *)nonce andState:(NSString *)state
{
#if AUTHENTICATION_SERVICES_AVAILABLE
if (@available(iOS 13.0, tvOS 13.0, macOS 10.15, *))
if (@available(iOS 13.0, tvOS 13.0, macOS 10.15, visionOS 1.0, *))
{
ASAuthorizationAppleIDRequest *request = [[self appleIdProvider] createRequest];
NSMutableArray *scopes = [NSMutableArray array];
Expand Down Expand Up @@ -152,7 +152,7 @@ - (void) loginWithAppleId:(uint)requestId withOptions:(AppleAuthManagerLoginOpti
- (void) getCredentialStateForUser:(NSString *)userId withRequestId:(uint)requestId
{
#if AUTHENTICATION_SERVICES_AVAILABLE
if (@available(iOS 13.0, tvOS 13.0, macOS 10.15, *))
if (@available(iOS 13.0, tvOS 13.0, macOS 10.15, visionOS 1.0, *))
{
[[self appleIdProvider] getCredentialStateForUserID:userId completion:^(ASAuthorizationAppleIDProviderCredentialState credentialState, NSError * _Nullable error) {
NSNumber *credentialStateNumber = nil;
Expand Down Expand Up @@ -185,7 +185,7 @@ - (void) getCredentialStateForUser:(NSString *)userId withRequestId:(uint)reques
- (void) registerCredentialsRevokedCallbackForRequestId:(uint)requestId
{
#if AUTHENTICATION_SERVICES_AVAILABLE
if (@available(iOS 13.0, tvOS 13.0, macOS 10.15, *))
if (@available(iOS 13.0, tvOS 13.0, macOS 10.15, visionOS 1.0, *))
{
if ([self credentialsRevokedObserver])
{
Expand Down Expand Up @@ -265,7 +265,7 @@ - (void) sendsLoginResponseInternalErrorWithCode:(NSInteger)code andMessage:(NSS
#if AUTHENTICATION_SERVICES_AVAILABLE

- (void) performAuthorizationRequestsForController:(ASAuthorizationController *)authorizationController withRequestId:(uint)requestId
API_AVAILABLE(ios(13.0), macos(10.15), tvos(13.0), watchos(6.0))
API_AVAILABLE(ios(13.0), macos(10.15), tvos(13.0), watchos(6.0), visionos(1.0))
{
NSValue *authControllerAsKey = [NSValue valueWithNonretainedObject:authorizationController];
[[self authorizationsInProgress] setObject:@(requestId) forKey:authControllerAsKey];
Expand All @@ -278,7 +278,7 @@ - (void) performAuthorizationRequestsForController:(ASAuthorizationController *)
#pragma mark ASAuthorizationControllerDelegate protocol implementation

- (void) authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization
API_AVAILABLE(ios(13.0), macos(10.15), tvos(13.0), watchos(6.0))
API_AVAILABLE(ios(13.0), macos(10.15), tvos(13.0), watchos(6.0), visionos(1.0))
{
NSValue *authControllerAsKey = [NSValue valueWithNonretainedObject:controller];
NSNumber *requestIdNumber = [[self authorizationsInProgress] objectForKey:authControllerAsKey];
Expand Down Expand Up @@ -306,7 +306,7 @@ - (void) authorizationController:(ASAuthorizationController *)controller didComp
}

- (void) authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error
API_AVAILABLE(ios(13.0), macos(10.15), tvos(13.0), watchos(6.0))
API_AVAILABLE(ios(13.0), macos(10.15), tvos(13.0), watchos(6.0), visionos(1.0))
{
NSValue *authControllerAsKey = [NSValue valueWithNonretainedObject:controller];
NSNumber *requestIdNumber = [[self authorizationsInProgress] objectForKey:authControllerAsKey];
Expand All @@ -326,9 +326,10 @@ - (void) authorizationController:(ASAuthorizationController *)controller didComp
#pragma mark ASAuthorizationControllerPresentationContextProviding protocol implementation

- (ASPresentationAnchor) presentationAnchorForAuthorizationController:(ASAuthorizationController *)controller
API_AVAILABLE(ios(13.0), macos(10.15), tvos(13.0), watchos(6.0))
API_AVAILABLE(ios(13.0), macos(10.15), tvos(13.0), watchos(6.0), visionos(1.0))
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 || __TV_OS_VERSION_MAX_ALLOWED >= 130000

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 || __TV_OS_VERSION_MAX_ALLOWED >= 130000 || __VISION_OS_VERSION_MAX_ALLOWED >= 10000
return [[[UIApplication sharedApplication] delegate] window];
#elif __MAC_OS_X_VERSION_MAX_ALLOWED >= 101500
return [[NSApplication sharedApplication] mainWindow];
Expand All @@ -343,9 +344,9 @@ - (ASPresentationAnchor) presentationAnchorForAuthorizationController:(ASAuthori

#pragma mark - Native C Calls

bool AppleAuth_IsCurrentPlatformSupported()
bool AppleAuth_IsCurrentPlatformSupported(void)
{
if (@available(iOS 13.0, tvOS 13.0, macOS 10.15, *))
if (@available(iOS 13.0, tvOS 13.0, macOS 10.15, visionOS 1.0, *))
{
return true;
}
Expand Down
Loading

0 comments on commit 87d01af

Please sign in to comment.