Skip to content
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

GETEDUROAM-35: Use of terms and required username suffix #3

Merged
merged 4 commits into from
Mar 17, 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
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@
{
"identity" : "xmlcoder",
"kind" : "remoteSourceControl",
"location" : "https://github.com/CoreOffice/XMLCoder.git",
"location" : "https://github.com/egeniq-forks/XMLCoder.git",
"state" : {
"revision" : "c438dad94f6a243b411b70a4b4bac54595064808",
"version" : "0.15.0"
"branch" : "cdata-fix",
"revision" : "43fea894c78be03d48b63833c969bb8a4a5f57c3"
}
}
],
Expand Down
4 changes: 3 additions & 1 deletion geteduroam/GeteduroamApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ struct GeteduroamApp: App {
var store: StoreOf<Main>!

init() {
store = .init(initialState: .init(), reducer: Main(authClient: appDelegate))
store = .init(initialState: .init(), reducer: Main(), prepareDependencies: { [appDelegate] in
$0.authClient = appDelegate
})
}

@StateObject var theme = Theme(
Expand Down
3 changes: 2 additions & 1 deletion geteduroam/GeteduroamPackage/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ let package = Package(
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "0.0.1"),
.package(url: "https://github.com/pointfreeco/swift-url-routing", from: "0.0.0"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "0.0.0"),
.package(url: "https://github.com/CoreOffice/XMLCoder.git", .upToNextMajor(from: "0.15.0"))
// .package(url: "https://github.com/CoreOffice/XMLCoder.git", .upToNextMajor(from: "0.15.0"))
.package(url: "https://github.com/egeniq-forks/XMLCoder.git", branch: "cdata-fix")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
13 changes: 13 additions & 0 deletions geteduroam/GeteduroamPackage/Sources/AuthClient/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@ import Foundation
public protocol AuthClient {
func startAuth(request: OIDAuthorizationRequest) async throws -> OIDAuthState
}

extension DependencyValues {
public var authClient: AuthClient {
get { self[AuthClientKey.self] }
set { self[AuthClientKey.self] = newValue }
}

public enum AuthClientKey: TestDependencyKey {
public static var testValue = mockClient
}
}

private let mockClient: AuthClient = FailingAuthClient()
53 changes: 29 additions & 24 deletions geteduroam/GeteduroamPackage/Sources/Connect/ConnectFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import SwiftUI
import XMLCoder

public struct Connect: Reducer {
public let authClient: AuthClient

public init(authClient: AuthClient = FailingAuthClient()) {
self.authClient = authClient
}
public init() { }

public struct State: Equatable {
public init(institution: Institution, loadingState: LoadingState = .initial, credentials: Credentials? = nil, destination: Destination.State? = nil) {
Expand All @@ -28,12 +24,21 @@ public struct Connect: Reducer {
public var providerInfo: ProviderInfo?
public var agreedToTerms: Bool = false
public var credentials: Credentials?
public var requiredUserNameSuffix: String? = nil
public var promptForCredentials: Bool = false

public var username: String {
credentials?.username ?? ""
}

public var usernamePrompt: String {
if let requiredUserNameSuffix {
return NSLocalizedString("Username", comment: "") + "@" + requiredUserNameSuffix
} else {
return NSLocalizedString("Username", comment: "")
}
}

public var password: String {
credentials?.password ?? ""
}
Expand Down Expand Up @@ -148,7 +153,6 @@ public struct Connect: Reducer {
public enum TermsAlertAction: Equatable {
case agreeButtonTapped
case disagreeButtonTapped
case readTermsButtonTapped
}

public var body: some Reducer<State, Action> {
Expand All @@ -174,30 +178,31 @@ public struct Connect: Reducer {
}
}

@Dependency(\.authClient) var authClient
@Dependency(\.dismiss) var dismiss
@Dependency(\.openURL) var openURL

private func connect(state: inout State) -> Effect<Connect.Action> {
guard let profile = state.selectedProfile else {
return .none
}

guard state.agreedToTerms || state.providerInfo?.localizedTermsOfUseURL == nil else {
guard state.agreedToTerms || state.providerInfo?.termsOfUse?.localized() == nil else {
let termsAlert = AlertState<Destination.TermsAlertAction>(
title: {
TextState("Terms of Use", bundle: .module)
}, actions: {
ButtonState(action: .send(.readTermsButtonTapped)) {
TextState("Read Terms of Use")
}
ButtonState(action: .send(.agreeButtonTapped)) {
TextState("Agree")
}
ButtonState(role: .cancel, action: .send(.disagreeButtonTapped)) {
TextState("Disagree")
}
}, message: {
TextState("You must agree to the terms of use before you can use this network.")
}, message: { [termsOfUse = state.providerInfo?.termsOfUse?.localized()] in
var message = "You must agree to the terms of use before you can use this network."
if let termsOfUse {
message = message + "\n\n" + termsOfUse
}
return TextState(message)
})
state.destination = .termsAlert(termsAlert)
return .none
Expand Down Expand Up @@ -233,15 +238,6 @@ public struct Connect: Reducer {
state.loadingState = .initial
state.agreedToTerms = false
return .none

case .readTermsButtonTapped:
state.loadingState = .initial
return .run { [localizedTermsOfUseURL = state.providerInfo?.localizedTermsOfUseURL] _ in
guard let localizedTermsOfUseURL = localizedTermsOfUseURL else {
return
}
await openURL(localizedTermsOfUseURL)
}
}

case .destination:
Expand All @@ -268,15 +264,23 @@ public struct Connect: Reducer {
state.providerInfo = providerInfo
return connect(state: &state)

case let .connectResponse(.failure(InstitutionSetupError.eapConfigurationFailed(EAPConfiguratorError.missingCredentials, providerInfo))):
case let .connectResponse(.failure(InstitutionSetupError.eapConfigurationFailed(EAPConfiguratorError.invalidUsername(suffix), providerInfo))):
state.providerInfo = providerInfo
state.promptForCredentials = true
state.requiredUserNameSuffix = suffix
return .none

case let .connectResponse(.failure(InstitutionSetupError.eapConfigurationFailed(EAPConfiguratorError.missingCredentials(_, requiredSuffix: suffix), providerInfo))):
state.providerInfo = providerInfo
state.promptForCredentials = true
state.requiredUserNameSuffix = suffix
return .none

case let .connectResponse(.failure(error)):
// Read providerinfo if error has it so we can populate helpdesk
state.providerInfo = (error as? InstitutionSetupError)?.providerInfo
state.loadingState = .failure
state.requiredUserNameSuffix = nil

let nserror = error as NSError
// Telling the user they cancelled isn't helping
Expand Down Expand Up @@ -318,6 +322,7 @@ public struct Connect: Reducer {

case .dismissPromptForCredentials:
state.promptForCredentials = false
state.requiredUserNameSuffix = nil
state.credentials = nil
state.destination = nil
state.loadingState = .initial
Expand Down Expand Up @@ -385,7 +390,7 @@ public struct Connect: Reducer {
throw InstitutionSetupError.noValidProviderFound(providerList.providers.first?.providerInfo)
}

guard agreedToTerms || firstValidProvider.providerInfo?.localizedTermsOfUseURL == nil else {
guard agreedToTerms || firstValidProvider.providerInfo?.termsOfUse?.localized() == nil else {
throw InstitutionSetupError.missingTermsAcceptance(firstValidProvider.providerInfo)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public struct ConnectView: View {
get: \.promptForCredentials,
send: Connect.Action.dismissPromptForCredentials),
actions: {
TextField("Username", text: viewStore.binding(
TextField(viewStore.usernamePrompt, text: viewStore.binding(
get: \.username,
send: Connect.Action.updateUsername))
.textContentType(.username)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,20 @@ public class EAPConfigurator {
credentials.username.isEmpty == false,
credentials.password.isEmpty == false {
if let requiredSuffix = clientSideCredential.innerIdentitySuffix {
// Add required suffix to username if user didn't specify host
if credentials.username.contains("@") {
username = credentials.username
} else {
username = credentials.username + "@" + requiredSuffix
}

if let hint = clientSideCredential.innerIdentityHint, hint == true {
// Add required suffix to username if user didn't specify host
if credentials.username.contains("@") {
username = credentials.username
} else {
username = credentials.username + "@" + requiredSuffix
}

guard username.hasSuffix("@\(requiredSuffix)") else {
throw EAPConfiguratorError.invalidUsername(suffix: requiredSuffix)
}
} else {
username = credentials.username

guard username.hasSuffix("@\(requiredSuffix)") || username.hasSuffix(".\(requiredSuffix)") else {
throw EAPConfiguratorError.invalidUsername(suffix: requiredSuffix)
}
Expand All @@ -224,7 +226,8 @@ public class EAPConfigurator {
}
password = credentials.password
} else {
throw EAPConfiguratorError.missingCredentials(clientSideCredential)
let requiredSuffix = clientSideCredential.innerIdentitySuffix
throw EAPConfiguratorError.missingCredentials(clientSideCredential, requiredSuffix: requiredSuffix)
}

let outerEapTypes = identityProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public enum EAPConfiguratorError: Error {
case cannotCopySupportedInterfaces

/// No credentials in configuration
case missingCredentials(ClientCredential)
case missingCredentials(ClientCredential, requiredSuffix: String?)

/// Username must end with
case invalidUsername(suffix: String)
Expand Down
8 changes: 2 additions & 6 deletions geteduroam/GeteduroamPackage/Sources/Main/MainFeature.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import AuthClient
import CacheClient
import ComposableArchitecture
import Connect
Expand All @@ -7,14 +6,11 @@ import Foundation
import Models

public struct Main: Reducer {
public let authClient: AuthClient
public init() { }

@Dependency(\.cacheClient) var cacheClient
@Dependency(\.discoveryClient) var discoveryClient

public init(authClient: AuthClient = FailingAuthClient()) {
self.authClient = authClient
}

public struct State: Equatable {
public init(searchQuery: String = "", institutions: IdentifiedArrayOf<Institution> = .init(uniqueElements: []), loadingState: LoadingState = .initial, destination: Destination.State? = nil) {
self.searchQuery = searchQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,3 @@ public struct ProviderInfo: Codable, Equatable {
case helpdesk = "Helpdesk"
}
}

extension ProviderInfo {
public var localizedTermsOfUseURL: URL? {
guard let urlString = termsOfUse?.localized() else {
return nil
}
return URL(string: urlString)
}
}
21 changes: 21 additions & 0 deletions geteduroam/GeteduroamPackage/Tests/ModelsTests/ModelsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ final class ModelsTests: XCTestCase {
let decoder = XMLDecoder()
decoder.shouldProcessNamespaces = true
decoder.dateDecodingStrategy = .iso8601

return decoder
}()

Expand Down Expand Up @@ -187,4 +188,24 @@ final class ModelsTests: XCTestCase {
webAddress: .init(string: "https://www.example.com"),
phone: nil)))
}

func testLocalizedProviderInfoWithCData() throws {
let sourceXML = """
<ProviderInfo>
<TermsOfUse lang="any"><![CDATA[Terms Of Use]]></TermsOfUse>
</ProviderInfo>
"""

let decoded = try decoder.decode(ProviderInfo.self, from: Data(sourceXML.utf8))

XCTAssertNoDifference(decoded, ProviderInfo(
displayName: nil,
description: nil,
providerLocations: [],
providerLogo: nil,
termsOfUse: [
.init(language: "any", value: "Terms Of Use")
],
helpdesk: nil))
}
}