|
| 1 | +// |
| 2 | +// OAuth2ImplicitGrantWithQueryParams.swift |
| 3 | +// OAuth2 |
| 4 | +// |
| 5 | +// Created by Tim Schmitz on 5/3/18. |
| 6 | +// Copyright 2018 Tim Schmitz |
| 7 | +// |
| 8 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +// you may not use this file except in compliance with the License. |
| 10 | +// You may obtain a copy of the License at |
| 11 | +// |
| 12 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +// |
| 14 | +// Unless required by applicable law or agreed to in writing, software |
| 15 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +// See the License for the specific language governing permissions and |
| 18 | +// limitations under the License. |
| 19 | +// |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +import Foundation |
| 24 | +#if !NO_MODULE_IMPORT |
| 25 | +import Base |
| 26 | +#endif |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + Class to handle OAuth2 implicit grant requests that return params in the query |
| 31 | + instead of the fragment. |
| 32 | + */ |
| 33 | +open class OAuth2ImplicitGrantWithQueryParams: OAuth2ImplicitGrant { |
| 34 | + |
| 35 | + override open func handleRedirectURL(_ redirect: URL) { |
| 36 | + logger?.debug("OAuth2", msg: "Handling redirect URL \(redirect.description)") |
| 37 | + do { |
| 38 | + // token should be in the URL query |
| 39 | + let comp = URLComponents(url: redirect, resolvingAgainstBaseURL: true) |
| 40 | + guard let query = comp?.query, query.count > 0 else { |
| 41 | + throw OAuth2Error.invalidRedirectURL(redirect.description) |
| 42 | + } |
| 43 | + |
| 44 | + let params = type(of: self).params(fromQuery: query) |
| 45 | + let dict = try parseAccessTokenResponse(params: params) |
| 46 | + logger?.debug("OAuth2", msg: "Successfully extracted access token") |
| 47 | + didAuthorize(withParameters: dict) |
| 48 | + } |
| 49 | + catch let error { |
| 50 | + didFail(with: error.asOAuth2Error) |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments