Skip to content

Add MFA Support to GoTrue #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/Supabase-Package.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,62 @@
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "GoTrue"
BuildableName = "GoTrue"
BlueprintName = "GoTrue"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "PostgREST"
BuildableName = "PostgREST"
BlueprintName = "PostgREST"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Realtime"
BuildableName = "Realtime"
BlueprintName = "Realtime"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Storage"
BuildableName = "Storage"
BlueprintName = "Storage"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand Down
29 changes: 29 additions & 0 deletions Examples/Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
7956406A2955AFBD0088A06F /* ErrorText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 795640692955AFBD0088A06F /* ErrorText.swift */; };
7956406D2955B3500088A06F /* SwiftUINavigation in Frameworks */ = {isa = PBXBuildFile; productRef = 7956406C2955B3500088A06F /* SwiftUINavigation */; };
795640702955B5190088A06F /* IdentifiedCollections in Frameworks */ = {isa = PBXBuildFile; productRef = 7956406F2955B5190088A06F /* IdentifiedCollections */; };
796298992AEBBA77000AA957 /* MFAFlow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 796298982AEBBA77000AA957 /* MFAFlow.swift */; };
7962989D2AEBC6F9000AA957 /* SVGView in Frameworks */ = {isa = PBXBuildFile; productRef = 7962989C2AEBC6F9000AA957 /* SVGView */; };
79719ECE2ADF26C400737804 /* Supabase in Frameworks */ = {isa = PBXBuildFile; productRef = 79719ECD2ADF26C400737804 /* Supabase */; };
79C591DC2AE0880F0088A9C8 /* ProductSampleApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79C591DB2AE0880F0088A9C8 /* ProductSampleApp.swift */; };
79C591DE2AE0880F0088A9C8 /* AppView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79C591DD2AE0880F0088A9C8 /* AppView.swift */; };
Expand Down Expand Up @@ -78,6 +80,8 @@
795640652955AE9C0088A06F /* TodoListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TodoListView.swift; sourceTree = "<group>"; };
795640672955AEB30088A06F /* Models.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Models.swift; sourceTree = "<group>"; };
795640692955AFBD0088A06F /* ErrorText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorText.swift; sourceTree = "<group>"; };
796298982AEBBA77000AA957 /* MFAFlow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MFAFlow.swift; sourceTree = "<group>"; };
7962989A2AEBBD9F000AA957 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
79C591D92AE0880F0088A9C8 /* ProductSample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ProductSample.app; sourceTree = BUILT_PRODUCTS_DIR; };
79C591DB2AE0880F0088A9C8 /* ProductSampleApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductSampleApp.swift; sourceTree = "<group>"; };
79C591DD2AE0880F0088A9C8 /* AppView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -109,6 +113,7 @@
files = (
795640702955B5190088A06F /* IdentifiedCollections in Frameworks */,
7956406D2955B3500088A06F /* SwiftUINavigation in Frameworks */,
7962989D2AEBC6F9000AA957 /* SVGView in Frameworks */,
79719ECE2ADF26C400737804 /* Supabase in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -231,6 +236,7 @@
793895C82954ABFF0044F2B8 /* Examples */ = {
isa = PBXGroup;
children = (
7962989A2AEBBD9F000AA957 /* Info.plist */,
793895CD2954AC000044F2B8 /* Assets.xcassets */,
7956405F2954AE140088A06F /* AuthView.swift */,
793895CF2954AC000044F2B8 /* Examples.entitlements */,
Expand All @@ -244,6 +250,7 @@
795640692955AFBD0088A06F /* ErrorText.swift */,
794EF1212955F26A008C9526 /* AddTodoListView.swift */,
794EF1232955F3DE008C9526 /* TodoListRow.swift */,
796298982AEBBA77000AA957 /* MFAFlow.swift */,
);
path = Examples;
sourceTree = "<group>";
Expand Down Expand Up @@ -339,6 +346,7 @@
7956406C2955B3500088A06F /* SwiftUINavigation */,
7956406F2955B5190088A06F /* IdentifiedCollections */,
79719ECD2ADF26C400737804 /* Supabase */,
7962989C2AEBC6F9000AA957 /* SVGView */,
);
productName = Examples;
productReference = 793895C62954ABFF0044F2B8 /* Examples.app */;
Expand Down Expand Up @@ -394,6 +402,7 @@
packageReferences = (
7956406B2955B3500088A06F /* XCRemoteSwiftPackageReference "swiftui-navigation" */,
7956406E2955B5190088A06F /* XCRemoteSwiftPackageReference "swift-identified-collections" */,
7962989B2AEBC6F9000AA957 /* XCRemoteSwiftPackageReference "SVGView" */,
);
productRefGroup = 793895C72954ABFF0044F2B8 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -432,6 +441,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
796298992AEBBA77000AA957 /* MFAFlow.swift in Sources */,
793895CC2954ABFF0044F2B8 /* RootView.swift in Sources */,
7956406A2955AFBD0088A06F /* ErrorText.swift in Sources */,
794EF1242955F3DE008C9526 /* TodoListRow.swift in Sources */,
Expand Down Expand Up @@ -534,6 +544,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -597,6 +608,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -624,6 +636,8 @@
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Examples/Info.plist;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.supabase.Examples;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand All @@ -642,6 +656,8 @@
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Examples/Info.plist;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.supabase.Examples;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down Expand Up @@ -743,6 +759,14 @@
minimumVersion = 1.0.0;
};
};
7962989B2AEBC6F9000AA957 /* XCRemoteSwiftPackageReference "SVGView" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/exyte/SVGView";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.0.6;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
Expand All @@ -756,6 +780,11 @@
package = 7956406E2955B5190088A06F /* XCRemoteSwiftPackageReference "swift-identified-collections" */;
productName = IdentifiedCollections;
};
7962989C2AEBC6F9000AA957 /* SVGView */ = {
isa = XCSwiftPackageProductDependency;
package = 7962989B2AEBC6F9000AA957 /* XCRemoteSwiftPackageReference "SVGView" */;
productName = SVGView;
};
79719ECD2ADF26C400737804 /* Supabase */ = {
isa = XCSwiftPackageProductDependency;
productName = Supabase;
Expand Down
14 changes: 13 additions & 1 deletion Examples/Examples/AuthView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import GoTrue
import SwiftUI

@MainActor
final class AuthController: ObservableObject {
@Published var session: Session?

Expand All @@ -18,6 +19,16 @@ final class AuthController: ObservableObject {

return id
}

func observeAuth() async {
for await event in await supabase.auth.onAuthStateChange() {
guard event == .signedIn || event == .signedOut else {
return
}

session = try? await supabase.auth.session
}
}
}

struct AuthView: View {
Expand Down Expand Up @@ -74,7 +85,8 @@ struct AuthView: View {
case .signIn:
try await supabase.auth.signIn(email: email, password: password)
case .signUp:
try await supabase.auth.signUp(email: email, password: password)
try await supabase.auth.signUp(
email: email, password: password, redirectTo: URL(string: "com.supabase.Examples://")!)
}
} catch {
withAnimation {
Expand Down
30 changes: 30 additions & 0 deletions Examples/Examples/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import SwiftUI

struct HomeView: View {
@EnvironmentObject var auth: AuthController

@State private var mfaStatus: MFAStatus?

var body: some View {
NavigationStack {
TodoListView()
Expand All @@ -21,6 +25,32 @@ struct HomeView: View {
}
}
}
.task {
mfaStatus = await verifyMFAStatus()
}
.sheet(unwrapping: $mfaStatus) { $mfaStatus in
MFAFlow(status: mfaStatus)
}
}

private func verifyMFAStatus() async -> MFAStatus? {
do {
let aal = try await supabase.auth.mfa.getAuthenticatorAssuranceLevel()
switch (aal.currentLevel, aal.nextLevel) {
case ("aal1", "aal1"):
return .unenrolled
case ("aal1", "aal2"):
return .unverified
case ("aal2", "aal2"):
return .verified
case ("aal2", "aal1"):
return .disabled
default:
return nil
}
} catch {
return nil
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions Examples/Examples/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.supabase.Examples://</string>
</array>
</dict>
</array>
</dict>
</plist>
Loading