Skip to content

Commit

Permalink
authentication with Firebase Auth and JWT (#35)
Browse files Browse the repository at this point in the history
# Authentication with Firebase Auth and JWT

## ♻️ Current situation & Problem
Please refer to these two discussion posts for context: 
https://github.com/orgs/CS342/discussions/78.
https://github.com/orgs/CS342/discussions/83


## ⚙️ Release Notes 
Modifications has been made to Prisma/Chat/ChatView
1. We generated JWT token in app in the `signInWithFirebase` function:
`token = try await Auth.auth().currentUser?.getIDToken()`.
2. Then, the token is sent to the frontend via a URL parameter: `if let
url = URL(string: "http://localhost:3000?token=\(token)")`
Successful implementation is reflected by passing a JWT token from Swift
to the web chat interface, which connects to the python backend and
prints "user successfully authenticated via JWT".

## 📚 Documentation
See in-line documentation

## ✅ Testing
This codebase has not been thoroughly tested. We will address the
stylistic requirements later.


## 📝 Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/CS342/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/CS342/.github/blob/main/CONTRIBUTING.md):
- [x] I agree to follow the [Code of
Conduct](https://github.com/CS342/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/CS342/.github/blob/main/CONTRIBUTING.md).

---------

Co-authored-by: bryant-jimenez <[email protected]>
Co-authored-by: Paul Schmiedmayer <[email protected]>
Co-authored-by: Dhruv Naik <[email protected]>
  • Loading branch information
4 people authored Mar 11, 2024
1 parent 257bbcd commit aa17157
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 23 deletions.
12 changes: 6 additions & 6 deletions Prisma.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
2F4E23832989D51F0013F3D9 /* PrismaTestingSetup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F4E23822989D51F0013F3D9 /* PrismaTestingSetup.swift */; };
2F4FC8D729EE69D300BFFE26 /* MockUpload.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F4FC8D629EE69D300BFFE26 /* MockUpload.swift */; };
2F5E32BD297E05EA003432F8 /* PrismaDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F5E32BC297E05EA003432F8 /* PrismaDelegate.swift */; };
2F6025CB29BBE70F0045459E /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 2F6025CA29BBE70F0045459E /* GoogleService-Info.plist */; };
2FA0BFED2ACC977500E0EF83 /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = 2FA0BFEC2ACC977500E0EF83 /* Localizable.xcstrings */; };
2FB099AF2A875DF100B20952 /* FirebaseAuth in Frameworks */ = {isa = PBXBuildFile; productRef = 2FB099AE2A875DF100B20952 /* FirebaseAuth */; };
2FB099B12A875DF100B20952 /* FirebaseFirestore in Frameworks */ = {isa = PBXBuildFile; productRef = 2FB099B02A875DF100B20952 /* FirebaseFirestore */; };
Expand Down Expand Up @@ -74,10 +73,11 @@
A9DFE8A92ABE551400428242 /* AccountButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9DFE8A82ABE551400428242 /* AccountButton.swift */; };
A9FE7AD02AA39BAB0077B045 /* AccountSheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9FE7ACF2AA39BAB0077B045 /* AccountSheet.swift */; };
AC69903E2B6C5A2F00D92970 /* PrivacyModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = AC69903D2B6C5A2F00D92970 /* PrivacyModule.swift */; };
ACB5DFBA2B9F9E76004F28E6 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = ACB5DFB92B9F9E76004F28E6 /* GoogleService-Info.plist */; };
D8027E912B90655700BB9466 /* ManageDataView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8027E902B90655700BB9466 /* ManageDataView.swift */; };
D8F136C52B85CEED000BA7AE /* DeleteDataView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8F136C42B85CEED000BA7AE /* DeleteDataView.swift */; };
E4C766262B72D50500C1DEDA /* WebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4C766252B72D50500C1DEDA /* WebView.swift */; };
F83B7CBE2B8FFE6800662914 /* PrismaStandard+TimeIndex.swift in Sources */ = {isa = PBXBuildFile; fileRef = F83B7CBD2B8FFE6800662914 /* PrismaStandard+TimeIndex.swift */; };
F83B7CBE2B8FFE6800662914 /* PrismaStandard+TimeIndex.swift in Sources */ = {isa = PBXBuildFile; fileRef = F83B7CBD2B8FFE6800662914 /* PrismaStandard+TimeIndex.swift */; };
F8AF6F9A2B5F2B1A0011C32D /* AppIcon-NoBG.png in Resources */ = {isa = PBXBuildFile; fileRef = F8AF6F992B5F2B1A0011C32D /* AppIcon-NoBG.png */; };
F8AF6F9F2B5F35400011C32D /* ChatView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8AF6F9E2B5F35400011C32D /* ChatView.swift */; };
F8AF6FA52B5F3AE70011C32D /* EventContextCard.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8AF6FA42B5F3AE70011C32D /* EventContextCard.swift */; };
Expand Down Expand Up @@ -116,7 +116,6 @@
2F4E23822989D51F0013F3D9 /* PrismaTestingSetup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrismaTestingSetup.swift; sourceTree = "<group>"; };
2F4FC8D629EE69D300BFFE26 /* MockUpload.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockUpload.swift; sourceTree = "<group>"; };
2F5E32BC297E05EA003432F8 /* PrismaDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrismaDelegate.swift; sourceTree = "<group>"; };
2F6025CA29BBE70F0045459E /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
2FA0BFEC2ACC977500E0EF83 /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = "<group>"; };
2FAEC07F297F583900C11C42 /* Prisma.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Prisma.entitlements; sourceTree = "<group>"; };
2FC94CD4298B0A1D009C8209 /* Prisma.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = Prisma.xctestplan; sourceTree = "<group>"; };
Expand Down Expand Up @@ -157,10 +156,11 @@
A9DFE8A82ABE551400428242 /* AccountButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountButton.swift; sourceTree = "<group>"; };
A9FE7ACF2AA39BAB0077B045 /* AccountSheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountSheet.swift; sourceTree = "<group>"; };
AC69903D2B6C5A2F00D92970 /* PrivacyModule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivacyModule.swift; sourceTree = "<group>"; };
ACB5DFB92B9F9E76004F28E6 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
D8027E902B90655700BB9466 /* ManageDataView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ManageDataView.swift; sourceTree = "<group>"; };
D8F136C42B85CEED000BA7AE /* DeleteDataView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeleteDataView.swift; sourceTree = "<group>"; };
E4C766252B72D50500C1DEDA /* WebView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebView.swift; sourceTree = "<group>"; };
F83B7CBD2B8FFE6800662914 /* PrismaStandard+TimeIndex.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PrismaStandard+TimeIndex.swift"; sourceTree = "<group>"; };
F83B7CBD2B8FFE6800662914 /* PrismaStandard+TimeIndex.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PrismaStandard+TimeIndex.swift"; sourceTree = "<group>"; };
F8AF6F992B5F2B1A0011C32D /* AppIcon-NoBG.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "AppIcon-NoBG.png"; sourceTree = "<group>"; };
F8AF6F9E2B5F35400011C32D /* ChatView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatView.swift; sourceTree = "<group>"; };
F8AF6FA42B5F3AE70011C32D /* EventContextCard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventContextCard.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -237,9 +237,9 @@
2FC9759D2978E30800BA99FE /* Supporting Files */ = {
isa = PBXGroup;
children = (
ACB5DFB92B9F9E76004F28E6 /* GoogleService-Info.plist */,
2FAEC07F297F583900C11C42 /* Prisma.entitlements */,
653A258928339462005D4D48 /* Info.plist */,
2F6025CA29BBE70F0045459E /* GoogleService-Info.plist */,
2F1AC9DE2B4E840E00C24973 /* Prisma.docc */,
);
path = "Supporting Files";
Expand Down Expand Up @@ -611,12 +611,12 @@
files = (
2FC3439229EE634B002D773C /* ConsentDocument.md in Resources */,
F8AF6F9A2B5F2B1A0011C32D /* AppIcon-NoBG.png in Resources */,
ACB5DFBA2B9F9E76004F28E6 /* GoogleService-Info.plist in Resources */,
F8AF6FAF2B5F42C40011C32D /* mid-day-en-US.json in Resources */,
F8AF6FAD2B5F42C40011C32D /* end-of-day-en-US.json in Resources */,
653A255528338800005D4D48 /* Assets.xcassets in Resources */,
F8AF6FAC2B5F42C40011C32D /* afternoon-en-US.json in Resources */,
2FA0BFED2ACC977500E0EF83 /* Localizable.xcstrings in Resources */,
2F6025CB29BBE70F0045459E /* GoogleService-Info.plist in Resources */,
F8AF6FAE2B5F42C40011C32D /* morning-en-US.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
2 changes: 1 addition & 1 deletion Prisma.xcodeproj/xcshareddata/xcschemes/Prisma.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
</CommandLineArgument>
<CommandLineArgument
argument = "--useFirebaseEmulator"
isEnabled = "NO">
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction>
Expand Down
52 changes: 42 additions & 10 deletions Prisma/Chat/ChatView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,68 @@
// SPDX-License-Identifier: MIT
//

import Firebase
import Foundation
import SpeziAccount
import SwiftUI
import WebKit


struct ChatView: View {
@Binding var presentingAccount: Bool
@State private var token: String?


var body: some View {
NavigationStack {
GeometryReader { geometry in
if let url = URL(string: "http://localhost:3000") {
WebView(url: url)
.navigationTitle("Chat")
.frame(
width: geometry.size.width,
height: geometry.size.height
)
// Fetch JWT token asynchronously
if let token = token {
if let url = URL(string: "http://localhost:3000?token=\(token)") { // this needs to be sent to the frontend
WebView(url: url)
.navigationTitle("Chat")
.frame(
width: geometry.size.width,
height: geometry.size.height
)
} else {
Text("Invalid URL")
}
} else {
Text("Invalid URL")
ProgressView()
}
}
/*
.onChange(of: account.signedIn) {
guard account.signedIn else {
return
}

Task {
try await self.signInWithFirebase()
}
}
*/
.task {
do {
try await self.getFirebaseIDToken()
} catch {
print("Firebase Auth failed \(error)")
}
}
}
}

init(presentingAccount: Binding<Bool>) {
self._presentingAccount = presentingAccount
}
}

extension ChatView {
func getFirebaseIDToken() async throws {
token = try await Auth.auth().currentUser?.getIDToken()
print("token is:", token ?? "")
}
}

#if DEBUG
struct ChatView_Previews: PreviewProvider {
Expand Down
3 changes: 2 additions & 1 deletion Prisma/PushNotifications/PushNotifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PrismaPushNotifications: NSObject, Module, NotificationHandler, Notificati

func handleNotificationAction(_ response: UNNotificationResponse) async {
// right now the default action is when a user taps on the notification. functionality can be expanded in the future.
// let actionIdentifier = response.actionIdentifier
_ = response.actionIdentifier
if let sentTimestamp = response.notification.request.content.userInfo["sent_timestamp"] as? String {
let openedTimestamp = Date().toISOFormat(timezone: TimeZone(abbreviation: "UTC"))
await standard.addNotificationOpenedTimestamp(timeSent: sentTimestamp, timeOpened: openedTimestamp)
Expand Down Expand Up @@ -85,6 +85,7 @@ class PrismaPushNotifications: NSObject, Module, NotificationHandler, Notificati
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
// Update the token in Firestore:
// The standard is an actor, which protects against data races and conforms to

// immutable data practice. Therefore we get into new asynchronous context and execute
Task {
await standard.storeToken(token: fcmToken)
Expand Down
3 changes: 3 additions & 0 deletions Prisma/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,9 @@
},
"Invalid URL" : {

},
"Invalid URL" : {

},
"JAMES_LANDAY_BIO" : {
"localizations" : {
Expand Down
10 changes: 5 additions & 5 deletions Prisma/SharedContext/FeatureFlags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ enum FeatureFlags {
static let showOnboarding = CommandLine.arguments.contains("--showOnboarding")
/// Disables the Firebase interactions, including the login/sign-up step and the Firebase Firestore upload.
static let disableFirebase = CommandLine.arguments.contains("--disableFirebase")
#if targetEnvironment(simulator)
/// Defines if the application should connect to the local firebase emulator. Always set to true when using the iOS simulator.
static let useFirebaseEmulator = true
#else
// if targetEnvironment(simulator)
// Defines if the application should connect to the local firebase emulator. Always set to true when using the iOS simulator.
// static let useFirebaseEmulator = true
// else
/// Defines if the application should connect to the local firebase emulator. Always set to true when using the iOS simulator.
static let useFirebaseEmulator = CommandLine.arguments.contains("--useFirebaseEmulator")
#endif
// endif
/// Adds a test task to the schedule at the current time
static let testSchedule = CommandLine.arguments.contains("--testSchedule")
}

0 comments on commit aa17157

Please sign in to comment.