Skip to content

Commit

Permalink
Merge pull request #535 from HackIllinois/sophia/auth-redesign
Browse files Browse the repository at this point in the history
Fixed login auth
  • Loading branch information
s0phialiu authored Sep 17, 2023
2 parents b0dcf91 + a5667b3 commit f543b1b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 30 deletions.
4 changes: 2 additions & 2 deletions HIAPI/Models/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import Foundation
public struct User: Codable, APIReturnable {
public let id: String
public let username: String
public let firstName: String
public let lastName: String
public let firstname: String
public let lastname: String
public let email: String
}

Expand Down
20 changes: 11 additions & 9 deletions HIAPI/Services/AuthService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,20 @@ public final class AuthService: BaseService {
}
}
}

// For HackIllinois 2024, we are only calling oauthURL because we do not make another call to get the token
public static func oauthURL(provider: OAuthProvider) -> URL {
guard let url = URL(string: AuthService.baseURL + "\(provider.rawValue)/?redirect_uri=https://hackillinois.org/auth/?isiOS=1") else {
fatalError("Invalid configuration.")
}
return url
guard let url = URL(string: AuthService.baseURL + "login/\(provider.rawValue)?device=ios") else {
fatalError("Invalid configuration.")
}
NSLog("using login URL" + "\(url)")
return url
}

public static func getAPIToken(provider: OAuthProvider, code: String) -> APIRequest<Token> {
var body = HTTPParameters()
body["code"] = code
return APIRequest<Token>(service: self, endpoint: "code/\(provider.rawValue)/?redirect_uri=https://hackillinois.org/auth/?isiOS=1", body: body, method: .POST)
public static func getAPIToken(provider: OAuthProvider) -> APIRequest<Token> {
let body = HTTPParameters()
let temp = APIRequest<Token>(service: self, endpoint: AuthService.baseURL + "login/\(provider.rawValue)?device=ios", body: body, method: .GET)
NSLog(temp.body?.description ?? "")
return temp
}

public static func getRoles() -> APIRequest<RolesContainer> {
Expand Down
2 changes: 1 addition & 1 deletion HIAPI/Services/BaseService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import APIManager

public class BaseService: APIService {
public class var baseURL: String {
return "https://api.hackillinois.org/"
return "https://adonix.hackillinois.org/"
}

public static var headers: HTTPHeaders? = ["Content-Type": "application/json"]
Expand Down
4 changes: 2 additions & 2 deletions HackIllinois.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@
DF37065A2925DE57000B4278 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DF3706592925DE57000B4278 /* QuartzCore.framework */; };
DF37065C2925DE5D000B4278 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DF37065B2925DE5D000B4278 /* SystemConfiguration.framework */; };
DF37065E2925DE66000B4278 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DF37065D2925DE66000B4278 /* UIKit.framework */; };
E1BF5E7A298B462F00A98390 /* HIStaffButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1BF5E79298B462F00A98390 /* HIStaffButtonView.swift */; };
DF5B27C5299197E80034A8B9 /* Array.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF5B27C4299197E80034A8B9 /* Array.swift */; };
DF5B27C7299372AE0034A8B9 /* Hack_PopcornCart_Text.json in Resources */ = {isa = PBXBuildFile; fileRef = DF5B27C6299372AE0034A8B9 /* Hack_PopcornCart_Text.json */; };
E170038A29679D08009B99A8 /* HIProfileCardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E170038929679D08009B99A8 /* HIProfileCardView.swift */; };
E1BF5E7A298B462F00A98390 /* HIStaffButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1BF5E79298B462F00A98390 /* HIStaffButtonView.swift */; };
E1E4A5D9291C9BF800780BA1 /* HICarouselSwiftUIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1E4A5D6291C9BF700780BA1 /* HICarouselSwiftUIView.swift */; };
E1E4A5DA291C9BF800780BA1 /* HIOnboardingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1E4A5D7291C9BF700780BA1 /* HIOnboardingView.swift */; };
E1E4A5DB291C9BF800780BA1 /* HIOnboardingViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1E4A5D8291C9BF700780BA1 /* HIOnboardingViewModel.swift */; };
Expand Down Expand Up @@ -357,10 +357,10 @@
DF3706592925DE57000B4278 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
DF37065B2925DE5D000B4278 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
DF37065D2925DE66000B4278 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
E1BF5E79298B462F00A98390 /* HIStaffButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HIStaffButtonView.swift; sourceTree = "<group>"; };
DF5B27C4299197E80034A8B9 /* Array.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Array.swift; sourceTree = "<group>"; };
DF5B27C6299372AE0034A8B9 /* Hack_PopcornCart_Text.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = Hack_PopcornCart_Text.json; sourceTree = "<group>"; };
E170038929679D08009B99A8 /* HIProfileCardView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HIProfileCardView.swift; sourceTree = "<group>"; };
E1BF5E79298B462F00A98390 /* HIStaffButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HIStaffButtonView.swift; sourceTree = "<group>"; };
E1E4A5D6291C9BF700780BA1 /* HICarouselSwiftUIView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HICarouselSwiftUIView.swift; sourceTree = "<group>"; };
E1E4A5D7291C9BF700780BA1 /* HIOnboardingView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HIOnboardingView.swift; sourceTree = "<group>"; };
E1E4A5D8291C9BF700780BA1 /* HIOnboardingViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HIOnboardingViewModel.swift; sourceTree = "<group>"; };
Expand Down
11 changes: 11 additions & 0 deletions HackIllinois/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
.authorize(with: user)
.launch()
}

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
// Handle the URL with your custom scheme
print("This is our url" + url.description)
if url.scheme == "hackillinois" {
// Extract and process the data from the URL, e.g., JWT token
// Perform the necessary actions based on the authentication result
return true
}
return false
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
setupNavigationBarAppearance()
Expand Down
32 changes: 16 additions & 16 deletions HackIllinois/FlowControllers/HILoginFlowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ extension HILoginFlowController {
// MARK: - Login Flow
private extension HILoginFlowController {
private func attemptOAuthLogin(buildingUser user: HIUser, profile: HIProfile, sender: HIBaseViewController) {

//GUEST (bypass auth)
if user.provider == .guest {
var guestUser = HIUser()
Expand All @@ -104,43 +103,44 @@ private extension HILoginFlowController {
}
return
}

let loginURL = HIAPI.AuthService.oauthURL(provider: user.provider)
loginSession = ASWebAuthenticationSession(url: loginURL, callbackURLScheme: nil) { [weak self] (url, error) in
NSLog(loginURL.description)
loginSession = ASWebAuthenticationSession(url: loginURL, callbackURLScheme: "hackillinois", completionHandler: { [weak self] (url, error) in
if let url = url,
let components = URLComponents(url: url, resolvingAgainstBaseURL: true),
let queryItems = components.queryItems,
let code = queryItems.first(where: { $0.name == "code" })?.value,
code.trimmingCharacters(in: .whitespacesAndNewlines) != "" {
var user = user
var profile = profile
user.oauthCode = code
profile.oauthCode = code
self?.exchangeOAuthCodeForAPIToken(buildingUser: user, profile: profile, sender: sender)
let token = queryItems.first(where: { $0.name == "token" })?.value, // Get token
token.trimmingCharacters(in: .whitespacesAndNewlines) != "" {
var user = user
var profile = profile
user.token = token
profile.token = token
self?.populateUserData(buildingUser: user, profile: profile, sender: sender) // Populate user
} else if let error = error {
if (error as? ASWebAuthenticationSessionError)?.code == ASWebAuthenticationSessionError.canceledLogin {
// do nothing
} else {
self?.presentAuthenticationFailure(withError: error, sender: sender)
}
self?.presentAuthenticationFailure(withError: error, sender: sender)}
} else {
let error = HIError.unknownAuthenticationError
self?.presentAuthenticationFailure(withError: error, sender: sender)
}
}
})
loginSession?.presentationContextProvider = self
loginSession?.start()
}

// For HackIllinois 2024, this function is not being used
private func exchangeOAuthCodeForAPIToken(buildingUser user: HIUser, profile: HIProfile, sender: HIBaseViewController) {
HIAPI.AuthService.getAPIToken(provider: user.provider, code: user.oauthCode)
HIAPI.AuthService.getAPIToken(provider: user.provider)
.onCompletion { [weak self] result in
do {
let (apiToken, _) = try result.get()
var user = user
var profile = profile
user.token = apiToken.token
profile.token = apiToken.token
NSLog(apiToken.token)
self?.populateUserData(buildingUser: user, profile: profile, sender: sender)
} catch {
self?.presentAuthenticationFailure(withError: error, sender: sender)
Expand All @@ -158,8 +158,8 @@ private extension HILoginFlowController {
var user = user
user.id = apiUser.id
user.username = apiUser.username
user.firstName = apiUser.firstName
user.lastName = apiUser.lastName
user.firstName = apiUser.firstname
user.lastName = apiUser.lastname
user.email = apiUser.email
self?.populateRoleData(buildingUser: user, profile: profile, sender: sender)
} catch {
Expand Down
1 change: 1 addition & 0 deletions HackIllinois/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<string>hackillinois</string>
</array>
</dict>
<dict/>
</array>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
Expand Down

0 comments on commit f543b1b

Please sign in to comment.