Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agios 414.webview #25

Merged
merged 4 commits into from Apr 10, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion AeroGearOAuth2.xcodeproj/project.pbxproj
Expand Up @@ -29,6 +29,7 @@
48CD52DD19B0C22A008D0694 /* OAuth2Session.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48CD52DC19B0C22A008D0694 /* OAuth2Session.swift */; };
48CD52DF19B0CB5A008D0694 /* OAuth2SessionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48CD52DE19B0CB5A008D0694 /* OAuth2SessionTest.swift */; };
48DE90A619CDEAEE007F9B21 /* KeycloakOAuth2Module.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48DE90A519CDEAEE007F9B21 /* KeycloakOAuth2Module.swift */; };
48F79D9D1ABC191E00D8B712 /* WebViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48F79D9C1ABC191E00D8B712 /* WebViewController.swift */; };
516C2B389D203ECEBD9E0815 /* Pods.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9ADB5FF0CB048392A40B2D60 /* Pods.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
6F1432B81A168594003BEE5B /* AeroGearOAuth2.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = 4833046219AF1635002F8DA9 /* AeroGearOAuth2.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -87,6 +88,7 @@
48CD52DC19B0C22A008D0694 /* OAuth2Session.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OAuth2Session.swift; sourceTree = "<group>"; };
48CD52DE19B0CB5A008D0694 /* OAuth2SessionTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OAuth2SessionTest.swift; sourceTree = "<group>"; };
48DE90A519CDEAEE007F9B21 /* KeycloakOAuth2Module.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeycloakOAuth2Module.swift; sourceTree = "<group>"; };
48F79D9C1ABC191E00D8B712 /* WebViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WebViewController.swift; sourceTree = "<group>"; };
9ADB5FF0CB048392A40B2D60 /* Pods.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B8AABF38B64CDFEB481203C1 /* Pods-AeroGearOAuth2Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AeroGearOAuth2Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-AeroGearOAuth2Tests/Pods-AeroGearOAuth2Tests.debug.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -134,7 +136,6 @@
4833046419AF1635002F8DA9 /* AeroGearOAuth2 */ = {
isa = PBXGroup;
children = (
4833046719AF1635002F8DA9 /* AeroGearOAuth2.h */,
4833046519AF1635002F8DA9 /* Supporting Files */,
4833049F19AF327D002F8DA9 /* AccountManager.swift */,
483304A419AF44DF002F8DA9 /* Config.swift */,
Expand All @@ -146,13 +147,15 @@
4814EF4A19C1C0FA008BAC4D /* UntrustedMemoryOAuth2Session.swift */,
4814EF4D19C1C10C008BAC4D /* FacebookOAuth2Module.swift */,
48DE90A519CDEAEE007F9B21 /* KeycloakOAuth2Module.swift */,
48F79D9C1ABC191E00D8B712 /* WebViewController.swift */,
);
path = AeroGearOAuth2;
sourceTree = "<group>";
};
4833046519AF1635002F8DA9 /* Supporting Files */ = {
isa = PBXGroup;
children = (
4833046719AF1635002F8DA9 /* AeroGearOAuth2.h */,
4833046619AF1635002F8DA9 /* Info.plist */,
);
name = "Supporting Files";
Expand Down Expand Up @@ -420,6 +423,7 @@
buildActionMask = 2147483647;
files = (
483304A019AF327D002F8DA9 /* AccountManager.swift in Sources */,
48F79D9D1ABC191E00D8B712 /* WebViewController.swift in Sources */,
485D73811A35BCBC0007D9EC /* OpenIDClaim.swift in Sources */,
48DE90A619CDEAEE007F9B21 /* KeycloakOAuth2Module.swift in Sources */,
48CD52DD19B0C22A008D0694 /* OAuth2Session.swift in Sources */,
Expand Down
9 changes: 8 additions & 1 deletion AeroGearOAuth2/Config.swift
Expand Up @@ -97,7 +97,13 @@ public class Config {
*/
public var accountId: String?

public init(base: String, authzEndpoint: String, redirectURL: String, accessTokenEndpoint: String, clientId: String, refreshTokenEndpoint: String? = nil, revokeTokenEndpoint: String? = nil, isOpenIDConnect:Bool? = nil, userInfoEndpoint: String? = nil, scopes: [String] = [], clientSecret: String? = nil, accountId: String? = nil) {
/**
Boolean to indicate to either used a webview (if true) or an external browser (by default, false)
for authorization code grant flow.
*/
public var isWebView: Bool = false

public init(base: String, authzEndpoint: String, redirectURL: String, accessTokenEndpoint: String, clientId: String, refreshTokenEndpoint: String? = nil, revokeTokenEndpoint: String? = nil, isOpenIDConnect:Bool = false, userInfoEndpoint: String? = nil, scopes: [String] = [], clientSecret: String? = nil, accountId: String? = nil, isWebView: Bool = false) {
self.baseURL = base
self.authzEndpoint = authzEndpoint
self.redirectURL = redirectURL
Expand All @@ -110,5 +116,6 @@ public class Config {
self.clientId = clientId
self.clientSecret = clientSecret
self.accountId = accountId
self.isWebView = isWebView
}
}
21 changes: 17 additions & 4 deletions AeroGearOAuth2/OAuth2Module.swift
Expand Up @@ -50,7 +50,7 @@ public class OAuth2Module: AuthzModule {
var applicationLaunchNotificationObserver: NSObjectProtocol?
var applicationDidBecomeActiveNotificationObserver: NSObjectProtocol?
var state: AuthorizationState

var webView: WebViewController?
/**
Initialize an OAuth2 module

Expand All @@ -72,7 +72,9 @@ public class OAuth2Module: AuthzModule {
}

self.config = config
// TODO use timeout config paramter
if config.isWebView {
self.webView = WebViewController()
}
self.http = Http(baseURL: config.baseURL, requestSerializer: requestSerializer, responseSerializer: responseSerializer)
self.state = .AuthorizationStateUnknown
}
Expand All @@ -90,6 +92,9 @@ public class OAuth2Module: AuthzModule {
// from the server.
applicationLaunchNotificationObserver = NSNotificationCenter.defaultCenter().addObserverForName(AGAppLaunchedWithURLNotification, object: nil, queue: nil, usingBlock: { (notification: NSNotification!) -> Void in
self.extractCode(notification, completionHandler: completionHandler)
if ( self.webView != nil ) {
UIApplication.sharedApplication().keyWindow?.rootViewController?.dismissViewControllerAnimated(true, nil)
}
})

// register to receive notification when the application becomes active so we
Expand All @@ -110,8 +115,16 @@ public class OAuth2Module: AuthzModule {
self.state = .AuthorizationStatePendingExternalApproval

// calculate final url
var params = "?scope=\(config.scope)&redirect_uri=\(config.redirectURL.urlEncode())&client_id=\(config.clientId)&response_type=code"
UIApplication.sharedApplication().openURL(NSURL(string: http.calculateURL(config.baseURL, url:config.authzEndpoint).absoluteString! + params)!)
let params = "?scope=\(config.scope)&redirect_uri=\(config.redirectURL.urlEncode())&client_id=\(config.clientId)&response_type=code"
let url = NSURL(string:http.calculateURL(config.baseURL, url:config.authzEndpoint).absoluteString! + params)
if let url = url {
if self.webView != nil {
self.webView!.targetURL = url
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(self.webView!, animated: true, completion: nil)
} else {
UIApplication.sharedApplication().openURL(url)
}
}
}

/**
Expand Down
43 changes: 43 additions & 0 deletions AeroGearOAuth2/WebViewController.swift
@@ -0,0 +1,43 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright Red Hat, Inc., and individual contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Foundation

import UIKit

class WebViewController: UIViewController, UIWebViewDelegate {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possible rename class to OAuthWebViewController so it is easier identifiable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we embed the oauth controller inside a navigation controller with a close button on the left to dismiss if the user decides to?

var targetURL : NSURL = NSURL()
var webView : UIWebView = UIWebView()

override internal func viewDidLoad() {
super.viewDidLoad()
webView.frame = UIScreen.mainScreen().applicationFrame
webView.scalesPageToFit = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

notice if the controller is switched in landscape the web view doesn't correctly "scale-to-fit"

webView.delegate = self
self.view.addSubview(webView)
loadAddressURL()
}

override internal func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func loadAddressURL() {
let req = NSURLRequest(URL: targetURL)
webView.loadRequest(req)
}
}