Skip to content

Commit

Permalink
add credential checking to api
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMoonThatRises committed Aug 19, 2024
1 parent 21cda13 commit 6fdd4cf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Sources/StudentVue/Extensions/XMLHash+parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ public extension XMLHash {
do {
for child in request.children {
for attr in XMLHash.errorAttributes {
guard let attrValue = child.element?.attribute(by: attr)?.text else {
guard let attrValue = child.element?.attribute(by: attr)?.text.lowercased() else {
continue
}

throw StudentVueApi.StudentVueErrors.soapError(attrValue)
if attrValue.contains("user id") || attrValue.contains("password") {
throw StudentVueApi.StudentVueErrors.invalidCredentials
} else {
throw StudentVueApi.StudentVueErrors.soapError(attrValue)
}
}
}
} catch let error as StudentVueApi.StudentVueErrors {
Expand Down
17 changes: 17 additions & 0 deletions Sources/StudentVue/SOAPApi/StudentVueApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@ public class StudentVueApi {
return try XMLHash.parse(soapString: result)
}

/// Checks validity of user credentials quickly
///
/// - Throws: `Error` some other error has occured when api request was sent
///
/// - Returns: Success or not
public func checkCredentials() async throws -> Bool {
do {
_ = try await xmlServiceRequest(methodName: .getSoundFileData)

return true
} catch StudentVueErrors.invalidCredentials {
return false
} catch {
throw error
}
}

/// Gets districts near the given zip code
///
/// - Parameter zip: The zip code to search for near-by districts that use StudentVue
Expand Down
3 changes: 3 additions & 0 deletions Sources/StudentVue/SOAPApi/StudentVueErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extension StudentVueApi {
case clientNotIntialised
case noUsername
case noPassword
case invalidCredentials
case soapError(String)
}
}
Expand All @@ -31,6 +32,8 @@ extension StudentVueApi.StudentVueErrors {
return "No username provided"
case .noPassword:
return "No password provided"
case .invalidCredentials:
return "Invalid user id or password"
case .soapError(let string):
return "Soap request returned error: \(string)"
}
Expand Down

0 comments on commit 6fdd4cf

Please sign in to comment.