Skip to content

Commit

Permalink
Added authorisedScope function
Browse files Browse the repository at this point in the history
Most recent 'Scope' string returned from Strava is stored and 'authorisedScope' function added to access the scope
  • Loading branch information
FleetPhil authored Jan 2, 2021
1 parent 82a6ca9 commit dfd8bad
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Sources/StravaSwift/StravaClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ open class StravaClient: NSObject {
fileprivate var stravaAccessUsage: [Int]?
fileprivate var stravaAccessLimit: [Int]?

// 'Scope' string returned from Stava with authorised permissions
fileprivate var stravaScope: String?

fileprivate override init() {}
fileprivate var config: StravaConfig?

Expand Down Expand Up @@ -80,6 +83,12 @@ extension StravaClient {
}
}

// MARK: Access authorised scope
extension StravaClient {
public func authorisedScope() -> [Scope] {
return stravaScope?.split(separator: ",").compactMap { scopeString in Scope(rawValue: String(scopeString)) } ?? []
}
}

//MARK:varConfig

Expand Down Expand Up @@ -165,7 +174,6 @@ extension StravaClient: ASWebAuthenticationPresentationContextProviding {
public func handleAuthorizationRedirect(_ url: URL) -> Bool {
if let redirectUri = config?.redirectUri, url.absoluteString.starts(with: redirectUri),
let params = url.getQueryParameters(), params["code"] != nil, params["scope"] != nil, params["state"] == "ios" {

self.handleAuthorizationRedirect(url) { result in
if let currentAuthorizationHandler = self.currentAuthorizationHandler {
currentAuthorizationHandler(result)
Expand All @@ -186,6 +194,7 @@ extension StravaClient: ASWebAuthenticationPresentationContextProviding {
**/
private func handleAuthorizationRedirect(_ url: URL, result: @escaping AuthorizationHandler) {
if let code = url.getQueryParameters()?["code"] {
self.stravaScope = url.getQueryParameters()?["scope"]
self.getAccessToken(code, result: result)
} else {
result(.failure(generateError(failureReason: "Invalid authorization code", response: nil)))
Expand Down

0 comments on commit dfd8bad

Please sign in to comment.