Skip to content

Commit 71ad611

Browse files
tschmitzp2
authored andcommitted
OAuth2ImplicitGrantWithQueryParams Subclass for a non-standard case (p2#262)
Handles APIs that return token and other info in the query instead of the fragment.
1 parent bbc6fa1 commit 71ad611

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Contributors
33

44
Contributors to the codebase, in reverse chronological order:
55

6+
- Tim Schmitz, @tschmitz
67
- Seb Skuse, @sebskuse
78
- David Hardiman, @dhardiman
89
- Amaury David, @amaurydavid
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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

Comments
 (0)