smartcar / ios-sdk

Smartcar SDK for iOS.
https://smartcar.github.io/ios-sdk/
MIT License
19 stars 7 forks source link

Invalid default state parameter #21

Closed dvkch closed 7 years ago

dvkch commented 7 years ago

Hello,

I have noticed the newly added state parameter for requests is not working properly:

I also tried setting it to " " but it prevents a valid URL from being created because it is not escaped properly, which causes a crash in:

let safariVC = SFSafariViewController(url: URL(string: authorizationURL)!)

Setting it to "%20" fixes the crash above, but the test in resumeAuthorizationFlow(with url: URL) doesn't handle escaping right and we end end comparing " " and "%20".

I set it to a random UUID string which seems to have fixes the problem.

I would highly recommend using a valid default value, or manage the case where the value is empty. To prevent crashes I would also suggest refactoring resumeAuthorizationFlow(with url: URL) as follows :

public func resumeAuthorizationFlow(with url: URL) throws -> String {
    let urlComp = URLComponents(url: url, resolvingAgainstBaseURL: false)
    guard let query = urlComp?.queryItems else {
        // declare a new error maybe?
        throw AuthorizationError.missingURL
    }

    guard let code = query.filter({ $0.name == "code" }).first?.value else {
        throw AuthorizationError.missingURL
    }

    guard let state = query.filter({ $0.name == "state" }).first?.value else {
        throw AuthorizationError.missingState
    }

    if state != self.request.state {
        throw AuthorizationError.invalidState
    }
    return code
}

Using query parameter names instead of probable indexes guarantees to find the right element if it exists, and this will prevent further crashes when accessing index out of bounds.

Thanks for considering,

Stan

jerzzhang commented 7 years ago

Created a PR regarding this issue #22