Skip to content

Migrate authentication to LabsPlatformSwift #600

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

jonathanmelitski
Copy link
Contributor

LabsPlatformSwift is a new package that can handle the authentication flow with the Penn Labs Platform server. It handles the general authentication flow and all network-related requests to the Penn Labs servers (all network requests have been rewritten).

The package has a lot of functionality. Analytics are at the crux of this package but are for a future PR.

Copy link
Member

@JHawk0224 JHawk0224 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is amazing!

For the status code comments, I don't think they need to be changed, I just wanted to point them out.

On an overall note, I see when logging in that the ProgressView we had inserted over the login sheet is no longer there (action is still disabled though preventing stale web request - ty). Is it possible we can add it back? Ik it's Penn's fault for being slow, but otherwise there's no indication something is happening.

Also, when testing, I just logged in but it brought be to the Login page after a successful login. Then clicking again doesn't do anything, see below video. Not sure why it's happening, but should be fixed.

weird-behavior.mov

Note that I am not done testing, so I will be continuing to add comments, but I thought I would share this review for now.

@@ -235,15 +236,7 @@ extension GSRController: GSRBookable {

alertController.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: {_ in }))
alertController.addAction(UIAlertAction(title: "Login", style: .default, handler: { _ in
let llc = LabsLoginController { (_) in
DispatchQueue.main.async {
self.submitBooking(for: GSRBooking(gid: gid, startTime: first.startTime, endTime: last.endTime, id: id, roomName: roomName))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What did this do? I would guess it's a completion for once the user is logged in, but in that case isn't the same logic needed after logging in with LabsPlatform?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preference I think. My thought is that one could opt for logging in, then immediately booking after, but I don't think that's a requirement. Also, I think this is such an edge case because the number of steps you'd need to go through to reach this case (you can't view availability as a guest) is a bit far-fetched. I think it's okay to prompt for a login, then have them hit the submit button again, though I can look into adding a continuation function like this in LabsPlatformSwift

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. You're right, I don't see the need to include it if it should be impossible to not be logged in on the book screen in the first place.

if accountID == nil {
FirebaseAnalyticsManager.shared.trackEvent(action: "Attempt Login", result: "Failed Login", content: "Failed Login")
} else {
FirebaseAnalyticsManager.shared.trackEvent(action: "Attempt Login", result: "Successful Login", content: "Successful Login")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add back in tracking for these in the new auth flow? I know we're missing a ton of logging as it stands, but while we're modifying this stuff I feel like it makes sense to keep.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I agree, also with the current issues with analytics I'm questioning how much we should be writing. (it has a max of 20gb lol and we will definitely fill that very quickly)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. I know it's kinda weird to have two analytics things going on at once, but I also think it would be nice to actually flush out all the Firebase logs too at some point.

import UIKit
import PennMobileShared

class PacCodeViewController: UIViewController, ShowsAlertForError, IndicatorEnabled {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will make another PR soon that recreates this in SwiftUI btw

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is big

var moreFeatures: [Feature] {
get {
let tabPreferences = UserDefaults.standard.getTabPreferences()
return dynamicFeatures.filter { !tabPreferences.contains($0) } + [.about]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait are these not being used by the SwiftUI More tab?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best I could tell, the dynamic features panel was rewritten in swiftUI already, will confirm

OAuth2NetworkManager.instance.getAccessToken { (token) in
guard let token = token else { return }

Task {
let url = URL(string: "https://pennmobile.org/api/login")!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this loginToBackend function still needed? It looks like it's only ever called in the apps initialization (which wouldn't make sense for logging in and out)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct; it would appear that it's no longer needed. I'm guessing I removed usages of it first, then rewrote that function anyways when I was writing in the new API calls.

Do you think it should be deleted?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes if it's not being used then it should be deleted

if successful {
UserDefaults.standard.setLastLogin()
}
UserDefaults.standard.storeCookies()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming we don't need to store cookies anymore and LabsPlatform handles everything?

Copy link
Contributor Author

@jonathanmelitski jonathanmelitski Apr 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually an incredible catch, forgot that HTTPCookieStorage doesn't persist through restarts, which is what LabsPlatform is currently doing. Yes it is handled by LabsPlatform (albeit evidently not to a full capacity) and will fix. Btw I'm going to PR my changes to LabsPlatformSwift just to add a reference.

@@ -7,11 +7,13 @@
//

import SwiftUI
import LabsPlatformSwift

struct LoggedOutView: View {
@EnvironmentObject var authManager: AuthManager
@Environment(\.colorScheme) var colorScheme
@State var isPresentingLoginSheet = false
Copy link
Member

@JHawk0224 JHawk0224 Apr 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you delete isPresentingLoginSheet? It's no longer being used

.sheet(isPresented: $isPresentingLoginSheet) {
LabsLoginView { success in
if success {
authManager.determineInitialState()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just trying to understand the new flow, but where is the equivalent to this in the new version? (on login success)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handled in AuthManager@78, the LabsPlatform library calls that function whenever global (logged in/logged out) auth state changes, so it will inherently be called on a successful login.

OAuth2NetworkManager.instance.clearRefreshToken()
OAuth2NetworkManager.instance.clearCurrentAccessToken()
if let platform = LabsPlatform.shared {
platform.logoutPlatform()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LabsPlatform.shared?.logoutPlatform()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a good Swift developer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol I was being very nitpicky, it just didn't match the use of LabsPlatform.shared?.loginWithPlatform()

@@ -146,8 +147,9 @@ class RootViewController: UIViewController, NotificationRequestable, ShowsAlert
func clearAccountData() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between this and the AuthManager's clearAccountData?

@jonathanmelitski
Copy link
Contributor Author

jonathanmelitski commented Apr 14, 2025

This is amazing!

For the status code comments, I don't think they need to be changed, I just wanted to point them out.

On an overall note, I see when logging in that the ProgressView we had inserted over the login sheet is no longer there (action is still disabled though preventing stale web request - ty). Is it possible we can add it back? Ik it's Penn's fault for being slow, but otherwise there's no indication something is happening.

Also, when testing, I just logged in but it brought be to the Login page after a successful login. Then clicking again doesn't do anything, see below video. Not sure why it's happening, but should be fixed.

weird-behavior.mov
Note that I am not done testing, so I will be continuing to add comments, but I thought I would share this review for now.

my thought/initial guess, the client id you're using isn't setup for the auth flow that I've been using. Oversight on my part, and it reminds me that this will need to be fixed prior to deployment.

@JHawk0224
Copy link
Member

This is amazing!
For the status code comments, I don't think they need to be changed, I just wanted to point them out.
On an overall note, I see when logging in that the ProgressView we had inserted over the login sheet is no longer there (action is still disabled though preventing stale web request - ty). Is it possible we can add it back? Ik it's Penn's fault for being slow, but otherwise there's no indication something is happening.
Also, when testing, I just logged in but it brought be to the Login page after a successful login. Then clicking again doesn't do anything, see below video. Not sure why it's happening, but should be fixed.
weird-behavior.mov
Note that I am not done testing, so I will be continuing to add comments, but I thought I would share this review for now.

my thought/initial guess, the client id you're using isn't setup for the auth flow that I've been using. Oversight on my part, and it reminds me that this will need to be fixed prior to deployment.

Ok I will test again when I get a chance.

@JHawk0224
Copy link
Member

JHawk0224 commented Apr 14, 2025

This is amazing!
For the status code comments, I don't think they need to be changed, I just wanted to point them out.
On an overall note, I see when logging in that the ProgressView we had inserted over the login sheet is no longer there (action is still disabled though preventing stale web request - ty). Is it possible we can add it back? Ik it's Penn's fault for being slow, but otherwise there's no indication something is happening.
Also, when testing, I just logged in but it brought be to the Login page after a successful login. Then clicking again doesn't do anything, see below video. Not sure why it's happening, but should be fixed.
weird-behavior.mov
Note that I am not done testing, so I will be continuing to add comments, but I thought I would share this review for now.

my thought/initial guess, the client id you're using isn't setup for the auth flow that I've been using. Oversight on my part, and it reminds me that this will need to be fixed prior to deployment.

Also re adding ProgressView for authentication with penn, it should be really easy. Basically just changing AuthWebView.swift in LabsPlatformSwift to use

ZStack {
    AuthWebViewRepresentable(url: url, redirect: redirect, isLoading: $isLoading, completion: callback)

    if isLoading {
        Color.black.opacity(0.1)
            .ignoresSafeArea()

        ProgressView()
            .tint(nil)
            .scaleEffect(1.6)
            .frame(width: 100, height: 100)
            .background(.thickMaterial)
            .cornerRadius(16)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants