Skip to content

Commit

Permalink
Added MultiAppSupport
Browse files Browse the repository at this point in the history
  • Loading branch information
“oleksii-minaiev” committed Jul 23, 2024
1 parent cb2d5f0 commit d18e2cb
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
16 changes: 13 additions & 3 deletions Sources/FronteggSwift/FronteggApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class FronteggApp {
public var auth: FronteggAuth
public var baseUrl: String = ""
public var clientId: String = ""
public var applicationId: String? = nil
public var bundleIdentifier: String = ""
public var embeddedMode: Bool = true
public var handleLoginWithSocialLogin:Bool = true
Expand Down Expand Up @@ -47,6 +48,7 @@ public class FronteggApp {
self.auth = FronteggAuth(
baseUrl: self.baseUrl,
clientId: self.clientId,
applicationId: self.applicationId,
credentialManager: self.credentialManager,
isRegional: false,
regionData: self.regionData,
Expand All @@ -66,20 +68,22 @@ public class FronteggApp {
self.auth = FronteggAuth(
baseUrl: self.baseUrl,
clientId: self.clientId,
applicationId: self.applicationId,
credentialManager: self.credentialManager,
isRegional:true,
isRegional: true,
regionData: self.regionData,
embeddedMode: self.embeddedMode
)

if let config = self.auth.selectedRegion {
self.baseUrl = config.baseUrl
self.clientId = config.clientId
self.applicationId = config.applicationId
self.auth.reinitWithRegion(config: config)

logger.info("Frontegg Initialized succcessfully (region: \(config.key))")
return;
}else {
} else {
// skip automatic authorize for regional config
self.auth.initializing = false
self.auth.isLoading = false
Expand All @@ -98,11 +102,13 @@ public class FronteggApp {

self.baseUrl = data.baseUrl
self.clientId = data.clientId
self.applicationId = data.applicationId


self.auth = FronteggAuth(
baseUrl: self.baseUrl,
clientId: self.clientId,
applicationId: self.applicationId,
credentialManager: self.credentialManager,
isRegional: false,
regionData: [],
Expand All @@ -119,16 +125,18 @@ public class FronteggApp {
public func manualInit(
baseUrl: String,
cliendId: String,
applicationId: String? = nil,
handleLoginWithSocialLogin: Bool = true,
handleLoginWithSSO:Bool = false
) {
self.baseUrl = baseUrl
self.clientId = cliendId
self.applicationId = applicationId

self.handleLoginWithSocialLogin = handleLoginWithSocialLogin
self.handleLoginWithSSO = handleLoginWithSSO

self.auth.manualInit(baseUrl: baseUrl, clientId: cliendId)
self.auth.manualInit(baseUrl: baseUrl, clientId: cliendId, applicationId: applicationId)
}
public func manualInitRegions(regions: [RegionConfig],
handleLoginWithSocialLogin: Bool = true,
Expand All @@ -139,6 +147,7 @@ public class FronteggApp {
self.auth.manualInitRegions(regions: regions)
self.baseUrl = self.auth.baseUrl
self.clientId = self.auth.clientId
self.applicationId = self.auth.applicationId
}

public func initWithRegion( regionKey:String ){
Expand All @@ -164,6 +173,7 @@ public class FronteggApp {

self.baseUrl = config.baseUrl
self.clientId = config.clientId
self.applicationId = config.applicationId
self.auth.reinitWithRegion(config: config)

logger.info("Frontegg Initialized succcessfully (region: \(regionKey))")
Expand Down
16 changes: 11 additions & 5 deletions Sources/FronteggSwift/FronteggAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class FronteggAuth: ObservableObject {
public var regionData: [RegionConfig]
public var baseUrl: String
public var clientId: String
public var applicationId: String? = nil
public var pendingAppLink: URL? = nil


Expand All @@ -52,6 +53,7 @@ public class FronteggAuth: ObservableObject {

init (baseUrl:String,
clientId: String,
applicationId: String?,
credentialManager: CredentialManager,
isRegional: Bool,
regionData: [RegionConfig],
Expand All @@ -65,7 +67,8 @@ public class FronteggAuth: ObservableObject {
self.embeddedMode = embeddedMode
self.baseUrl = baseUrl
self.clientId = clientId
self.api = Api(baseUrl: self.baseUrl, clientId: self.clientId)
self.applicationId = applicationId
self.api = Api(baseUrl: self.baseUrl, clientId: self.clientId, applicationId: self.applicationId)
self.selectedRegion = self.getSelectedRegion()

if ( isRegional || isLateInit == true ) {
Expand All @@ -78,12 +81,13 @@ public class FronteggAuth: ObservableObject {
self.initializeSubscriptions()
}

public func manualInit(baseUrl:String, clientId:String) {
public func manualInit(baseUrl:String, clientId:String, applicationId: String?) {
self.lateInit = false
self.baseUrl = baseUrl
self.clientId = clientId
self.applicationId = applicationId
self.isRegional = false
self.api = Api(baseUrl: self.baseUrl, clientId: self.clientId)
self.api = Api(baseUrl: self.baseUrl, clientId: self.clientId, applicationId: self.applicationId)
self.initializeSubscriptions()
}

Expand All @@ -97,7 +101,8 @@ public class FronteggAuth: ObservableObject {
if let config = self.selectedRegion {
self.baseUrl = config.baseUrl
self.clientId = config.clientId
self.api = Api(baseUrl: self.baseUrl, clientId: self.clientId)
self.applicationId = config.applicationId
self.api = Api(baseUrl: self.baseUrl, clientId: self.clientId, applicationId: self.applicationId)
self.initializeSubscriptions()
}
}
Expand All @@ -106,8 +111,9 @@ public class FronteggAuth: ObservableObject {
public func reinitWithRegion(config:RegionConfig) {
self.baseUrl = config.baseUrl
self.clientId = config.clientId
self.applicationId = config.applicationId
self.selectedRegion = config
self.api = Api(baseUrl: self.baseUrl, clientId: self.clientId)
self.api = Api(baseUrl: self.baseUrl, clientId: self.clientId, applicationId: self.applicationId)

self.initializeSubscriptions()
}
Expand Down
17 changes: 14 additions & 3 deletions Sources/FronteggSwift/services/Api.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ public class Api {
private let logger = getLogger("Api")
private let baseUrl: String
private let clientId: String
private let applicationId: String?
private var cookieName: String
private let credentialManager: CredentialManager

init(baseUrl: String, clientId: String) {
init(baseUrl: String, clientId: String, applicationId: String?) {
self.baseUrl = baseUrl
self.clientId = clientId
self.applicationId = applicationId
self.credentialManager = CredentialManager(serviceKey: "frontegg")

self.cookieName = "fe_refresh_\(clientId)"
Expand All @@ -39,7 +41,10 @@ public class Api {
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue(self.baseUrl, forHTTPHeaderField: "Origin")


if (self.applicationId != nil) {
request.setValue(self.applicationId, forHTTPHeaderField: "frontegg-requested-application-id")
}

if let accessToken = try? credentialManager.get(key: KeychainKeys.accessToken.rawValue) {
request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "authorization")
}
Expand All @@ -61,6 +66,10 @@ public class Api {
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue(self.baseUrl, forHTTPHeaderField: "Origin")

if (self.applicationId != nil) {
request.setValue(self.applicationId, forHTTPHeaderField: "frontegg-requested-application-id")
}

additionalHeaders.forEach({ (key: String, value: String) in
request.setValue(value, forHTTPHeaderField: key)
})
Expand Down Expand Up @@ -92,6 +101,9 @@ public class Api {
let cookieHeaderValue = "\(self.cookieName)=\(refreshToken!)"
request.setValue(cookieHeaderValue, forHTTPHeaderField: "cookie")
}
if (self.applicationId != nil) {
request.setValue(self.applicationId, forHTTPHeaderField: "frontegg-requested-application-id")
}
request.httpMethod = "GET"

return try await URLSession.shared.data(for: request)
Expand Down Expand Up @@ -169,5 +181,4 @@ public class Api {
}



}
2 changes: 1 addition & 1 deletion Sources/FronteggSwift/utils/AuthorizeUrlGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class AuthorizeUrlGenerator {
var queryParams = [
URLQueryItem(name: "redirect_uri", value: redirectUri),
URLQueryItem(name: "response_type", value: "code"),
URLQueryItem(name: "client_id", value: FronteggApp.shared.clientId),
URLQueryItem(name: "client_id", value: FronteggApp.shared.applicationId ?? FronteggApp.shared.clientId),
URLQueryItem(name: "scope", value: "openid email profile"),
URLQueryItem(name: "code_challenge", value: codeChallenge),
URLQueryItem(name: "code_challenge_method", value: "S256"),
Expand Down
19 changes: 12 additions & 7 deletions Sources/FronteggSwift/utils/PlistHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ public struct RegionConfig {
public var key: String
public var baseUrl: String
public var clientId: String
public var applicationId: String?

public init(key: String, baseUrl: String, clientId: String){
public init(key: String, baseUrl: String, clientId: String, applicationId: String?){
self.key = key
self.baseUrl = baseUrl
self.clientId = clientId
self.applicationId = applicationId
}
}

struct PlistHelper {

private static var logLevelCache: Logger.Level? = nil

public static func fronteggConfig() throws -> (clientId: String, baseUrl: String, keychainService: String, bundleIdentifier: String) {
public static func fronteggConfig() throws -> (clientId: String, baseUrl: String, applicationId: String?, keychainService: String, bundleIdentifier: String) {
let bundle = Bundle.main;

let resourceName = (getenv("frontegg-testing") != nil) ? "FronteggTest" : "Frontegg"
Expand All @@ -48,9 +50,11 @@ struct PlistHelper {
throw FronteggError.configError(errorMessage)
}

let applicationId = values["applicationId"] as? String

let keychainService = values["keychainService"] as? String ?? "frontegg"

return (clientId: clientId, baseUrl: baseUrl, keychainService: keychainService, bundleIdentifier: bundle.bundleIdentifier!)
return (clientId: clientId, baseUrl: baseUrl, applicationId: applicationId, keychainService: keychainService, bundleIdentifier: bundle.bundleIdentifier!)
}

public static func fronteggRegionalConfig() throws -> (regions: [RegionConfig], keychainService: String, bundleIdentifier: String) {
Expand All @@ -77,11 +81,12 @@ struct PlistHelper {

let regionConfigs = try regions.map { dict in
guard let key = dict["key"],
let baseUrl = dict["baseUrl"],
let baseUrl = dict["baseUrl"],
let clientId = dict["clientId"] else {
throw FronteggError.configError("Frontegg.plist file at \(path) has invalid regions data, regions must be array of (key, baseUrl, clientId)")
}
return RegionConfig(key: key, baseUrl: baseUrl, clientId: clientId)
throw FronteggError.configError("Frontegg.plist file at \(path) has invalid regions data, regions must be array of (key, baseUrl, clientId)")
}
let applicationId = dict["applicationId"]
return RegionConfig(key: key, baseUrl: baseUrl, clientId: clientId, applicationId: applicationId)
}
return (regions: regionConfigs, keychainService: keychainService, bundleIdentifier: bundle.bundleIdentifier!)
}
Expand Down

0 comments on commit d18e2cb

Please sign in to comment.