Skip to content

Commit

Permalink
Merge pull request #93 from ibm-cloud-security/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
vitalymibm committed Apr 9, 2017
2 parents ba516af + 201990d commit 25fe420
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 44 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BluemixAppID.xcodeproj/
BluemixAppID.xcworkspace/
Pods/
Podfile.lock
2 changes: 0 additions & 2 deletions BluemixAppID.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
EF4994821E55F7BE008ACC27 /* AppIDTestConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = EFBA1FE91E51F7D9000EB3F5 /* AppIDTestConstants.swift */; };
EF4994831E55F7BE008ACC27 /* AppIDTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EFBA1FEA1E51F7D9000EB3F5 /* AppIDTests.swift */; };
EF4994841E55F7BE008ACC27 /* AuthorizationHeaderHelperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EFBA1FEB1E51F7D9000EB3F5 /* AuthorizationHeaderHelperTests.swift */; };
EF4994851E55F7BE008ACC27 /* Info.plist in Sources */ = {isa = PBXBuildFile; fileRef = EFBA1FEC1E51F7D9000EB3F5 /* Info.plist */; };
EF4994861E55F7BE008ACC27 /* PreferencesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EFBA1FED1E51F7D9000EB3F5 /* PreferencesTests.swift */; };
EF4994871E55F7BE008ACC27 /* TokenManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EFBA1FEE1E51F7D9000EB3F5 /* TokenManagerTests.swift */; };
EF4994881E55F7BE008ACC27 /* TokenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EFBA1FEF1E51F7D9000EB3F5 /* TokenTests.swift */; };
Expand Down Expand Up @@ -645,7 +644,6 @@
EF4994831E55F7BE008ACC27 /* AppIDTests.swift in Sources */,
BDF5878B1E65736B00393C0C /* (null) in Sources */,
EF4994841E55F7BE008ACC27 /* AuthorizationHeaderHelperTests.swift in Sources */,
EF4994851E55F7BE008ACC27 /* Info.plist in Sources */,
EF4994861E55F7BE008ACC27 /* PreferencesTests.swift in Sources */,
EF4994871E55F7BE008ACC27 /* TokenManagerTests.swift in Sources */,
EF4994881E55F7BE008ACC27 /* TokenTests.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion BluemixAppIDTests/AuthorizationManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public class AuthorizationManagerTests : XCTestCase {

}

var del = delegate()
let del = delegate()

// happy flow:
let redirect = AppIDConstants.REDIRECT_URI_VALUE
Expand Down
38 changes: 38 additions & 0 deletions BluemixAppIDTests/AuthorizationUIManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,44 @@ public class AuthorizationUIManagerTests: XCTestCase {


}

func testApplicationErr3() {
class MockRegistrationManager:RegistrationManager {
static var expectation:XCTestExpectation?
public override func clearRegistrationData() {
MockRegistrationManager.expectation?.fulfill()
}
}

class MockAuthorizationManager:BluemixAppID.AuthorizationManager {
static var expectation:XCTestExpectation?
public override func launchAuthorizationUI(accessTokenString: String?, authorizationDelegate: AuthorizationDelegate) {
XCTAssertNil(accessTokenString)
MockAuthorizationManager.expectation?.fulfill()
}
}
let expectation1 = expectation(description: "clear data")
let expectation2 = expectation(description: "invoke registration")
oauthManager.registrationManager = MockRegistrationManager(oauthManager:oauthManager)
MockRegistrationManager.expectation = expectation1
oauthManager.authorizationManager = MockAuthorizationManager(oAuthManager: oauthManager)
MockAuthorizationManager.expectation = expectation2

let manager = AuthorizationUIManager(oAuthManager: oauthManager, authorizationDelegate: delegate(exp: expectation1, errMsg: "Failed to obtain access and identity tokens"), authorizationUrl: "someurl", redirectUri: "someredirect")
manager.loginView = MockSafariView(url:URL(string: "http://www.someurl.com")!)
XCTAssertFalse(manager.application(UIApplication.shared, open: URL(string:AppIDConstants.REDIRECT_URI_VALUE.lowercased() + "?code=somecode&error=invalid_client")!, options: [:]))

waitForExpectations(timeout: 1) { error in
if let error = error {
XCTFail("err: \(error)")
}
}




}



}
44 changes: 22 additions & 22 deletions BluemixAppIDTests/UserAttributeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class UserAttributeTests: XCTestCase {
func onSuccess(result: [String:Any]) {
XCTAssert(result["key"] != nil)
let actualValue = result["key"]!
var actualValueString = String(describing: actualValue)
let actualValueString = String(describing: actualValue)
XCTAssert(actualValueString == "value")
passed = true
}
Expand All @@ -68,9 +68,9 @@ public class UserAttributeTests: XCTestCase {
}

func testGetAllAttributes () {
var delegate = MyDelegate()
var userAttributeManager = MockUserAttributeManger(appId: AppID.sharedInstance)
var resp = HTTPURLResponse(url: URL(string: "http://someurl.com")!, statusCode: 200, httpVersion: "1.1", headerFields: [:])
let delegate = MyDelegate()
let userAttributeManager = MockUserAttributeManger(appId: AppID.sharedInstance)
let resp = HTTPURLResponse(url: URL(string: "http://someurl.com")!, statusCode: 200, httpVersion: "1.1", headerFields: [:])
userAttributeManager.response = resp
userAttributeManager.data = "{\"key\" : \"value\"}".data(using: .utf8)
userAttributeManager.expectMethod = "GET"
Expand All @@ -90,9 +90,9 @@ public class UserAttributeTests: XCTestCase {
}

func testGetAllAttributesWithToken () {
var delegate = MyDelegate()
var userAttributeManager = MockUserAttributeManger(appId: AppID.sharedInstance)
var resp = HTTPURLResponse(url: URL(string: "http://someurl.com")!, statusCode: 200, httpVersion: "1.1", headerFields: [:])
let delegate = MyDelegate()
let userAttributeManager = MockUserAttributeManger(appId: AppID.sharedInstance)
let resp = HTTPURLResponse(url: URL(string: "http://someurl.com")!, statusCode: 200, httpVersion: "1.1", headerFields: [:])
userAttributeManager.response = resp
userAttributeManager.data = "{\"key\" : \"value\"}".data(using: .utf8)
userAttributeManager.token = "token"
Expand All @@ -113,9 +113,9 @@ public class UserAttributeTests: XCTestCase {


func testGetAttribute () {
var delegate = MyDelegate()
var userAttributeManager = MockUserAttributeManger(appId: AppID.sharedInstance)
var resp = HTTPURLResponse(url: URL(string: "http://someurl.com")!, statusCode: 200, httpVersion: "1.1", headerFields: [:])
let delegate = MyDelegate()
let userAttributeManager = MockUserAttributeManger(appId: AppID.sharedInstance)
let resp = HTTPURLResponse(url: URL(string: "http://someurl.com")!, statusCode: 200, httpVersion: "1.1", headerFields: [:])
userAttributeManager.response = resp
userAttributeManager.data = "{\"key\" : \"value\"}".data(using: .utf8)
userAttributeManager.expectMethod = "GET"
Expand All @@ -135,9 +135,9 @@ public class UserAttributeTests: XCTestCase {
}

func testGetAttributeWithToken () {
var delegate = MyDelegate()
var userAttributeManager = MockUserAttributeManger(appId: AppID.sharedInstance)
var resp = HTTPURLResponse(url: URL(string: "http://someurl.com")!, statusCode: 200, httpVersion: "1.1", headerFields: [:])
let delegate = MyDelegate()
let userAttributeManager = MockUserAttributeManger(appId: AppID.sharedInstance)
let resp = HTTPURLResponse(url: URL(string: "http://someurl.com")!, statusCode: 200, httpVersion: "1.1", headerFields: [:])
userAttributeManager.response = resp
userAttributeManager.data = "{\"key\" : \"value\"}".data(using: .utf8)
userAttributeManager.token = "token"
Expand All @@ -159,9 +159,9 @@ public class UserAttributeTests: XCTestCase {


func testSetAttribute () {
var delegate = MyDelegate()
var userAttributeManager = MockUserAttributeManger(appId: AppID.sharedInstance)
var resp = HTTPURLResponse(url: URL(string: "http://someurl.com")!, statusCode: 200, httpVersion: "1.1", headerFields: [:])
let delegate = MyDelegate()
let userAttributeManager = MockUserAttributeManger(appId: AppID.sharedInstance)
let resp = HTTPURLResponse(url: URL(string: "http://someurl.com")!, statusCode: 200, httpVersion: "1.1", headerFields: [:])
userAttributeManager.response = resp
userAttributeManager.data = "{\"key\" : \"value\"}".data(using: .utf8)
userAttributeManager.expectMethod = "PUT"
Expand All @@ -181,9 +181,9 @@ public class UserAttributeTests: XCTestCase {
}

func testSetAttributeWithToken () {
var delegate = MyDelegate()
var userAttributeManager = MockUserAttributeManger(appId: AppID.sharedInstance)
var resp = HTTPURLResponse(url: URL(string: "http://someurl.com")!, statusCode: 200, httpVersion: "1.1", headerFields: [:])
let delegate = MyDelegate()
let userAttributeManager = MockUserAttributeManger(appId: AppID.sharedInstance)
let resp = HTTPURLResponse(url: URL(string: "http://someurl.com")!, statusCode: 200, httpVersion: "1.1", headerFields: [:])
userAttributeManager.response = resp
userAttributeManager.data = "{\"key\" : \"value\"}".data(using: .utf8)
userAttributeManager.token = "token"
Expand All @@ -205,9 +205,9 @@ public class UserAttributeTests: XCTestCase {
}

func testFailure () {
var delegate = MyDelegate()
var userAttributeManager = MockUserAttributeManger(appId: AppID.sharedInstance)
var resp = HTTPURLResponse(url: URL(string: "http://someurl.com")!, statusCode: 404, httpVersion: "1.1", headerFields: [:])
let delegate = MyDelegate()
let userAttributeManager = MockUserAttributeManger(appId: AppID.sharedInstance)
let resp = HTTPURLResponse(url: URL(string: "http://someurl.com")!, statusCode: 404, httpVersion: "1.1", headerFields: [:])
userAttributeManager.response = resp
userAttributeManager.data = "{\"error\" : \"NOT_FOUND\"}".data(using: .utf8)
userAttributeManager.token = "token"
Expand Down
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,18 @@ import BluemixAppID
```
4. Initialize the client SDK by passing the tenantId and region parameters to the initialize method. A common, though not mandatory, place to put the initialization code is in the application:didFinishLaunchingWithOptions: method of the AppDelegate in your Swift application.
```swift
AppID.sharedInstance.initialize(tenantId: <tenantId>, bluemixRegion: AppID.<region>)
AppID.sharedInstance.initialize(tenantId: <tenantId>, bluemixRegion: AppID.REGION_UK)
```
* Replace "tenantId" with the App ID service tenantId.
* Replace the AppID.REGION_UK with the your App ID region (AppID.REGION_US_SOUTH, AppID.REGION_SYDNEY).

5. Add the following code to you AppDelegate file
```swift
func application(_ application: UIApplication, open url: URL, options :[UIApplicationOpenURLOptionsKey : Any]) -> Bool {
return AppID.sharedInstance.application(application, open: url, options: options)
}
```


* Replace ״tenantId״ with the App ID service tenantId. <br />
* Replace ״region״ with the App ID region.

For more information on obtaining these values see Before you begin.

### Using Login Widget
After the App ID client SDK is initialized, you can start authenticate users by launching the Login Widget.
Add the following import to the file in which you want to use the using the login Widget:
Expand Down Expand Up @@ -90,7 +87,7 @@ import BluemixAppID
```
Then add the following code:
```swift
BMSClient.sharedInstance.initialize(bluemixRegion: AppID.<region>)
BMSClient.sharedInstance.initialize(bluemixRegion: AppID.REGION_UK)
BMSClient.sharedInstance.authorizationManager = AppIDAuthorizationManager(appid:AppID.sharedInstance)
var request:Request = Request(url: "<your protected resource url>")
request.send(completionHandler: {(response:Response?, error:Error?) in
Expand Down
24 changes: 15 additions & 9 deletions Source/BluemixAppID/internal/AuthorizationUIManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ public class AuthorizationUIManager {
AuthorizationUIManager.logger.debug(message: "Launching safari view")
loginView = safariView(url: URL(string: authorizationUrl )!)
loginView?.authorizationDelegate = authorizationDelegate
let mainView = UIApplication.shared.keyWindow?.rootViewController
let rootView = UIApplication.shared.keyWindow?.rootViewController
let currentView = rootView?.presentedViewController
DispatchQueue.main.async {
mainView?.present(self.loginView!, animated: true, completion: nil)
currentView?.present(self.loginView!, animated: true, completion: nil)
}
}

Expand All @@ -58,12 +59,17 @@ public class AuthorizationUIManager {

if let err = Utils.getParamFromQuery(url: url, paramName: "error") {
loginView?.dismiss(animated: true, completion: { () -> Void in
let errorDescription = Utils.getParamFromQuery(url: url, paramName: "error_description")
let errorCode = Utils.getParamFromQuery(url: url, paramName: "error_code")
AuthorizationUIManager.logger.error(message: "error: " + err)
AuthorizationUIManager.logger.error(message: "errorCode: " + (errorCode ?? "not available"))
AuthorizationUIManager.logger.error(message: "errorDescription: " + (errorDescription ?? "not available"))
self.authorizationDelegate.onAuthorizationFailure(error: AuthorizationError.authorizationFailure("Failed to obtain access and identity tokens"))
if err == "invalid_client" {
self.oAuthManager.registrationManager?.clearRegistrationData()
self.oAuthManager.authorizationManager?.launchAuthorizationUI(authorizationDelegate: self.authorizationDelegate)
} else {
let errorDescription = Utils.getParamFromQuery(url: url, paramName: "error_description")
let errorCode = Utils.getParamFromQuery(url: url, paramName: "error_code")
AuthorizationUIManager.logger.error(message: "error: " + err)
AuthorizationUIManager.logger.error(message: "errorCode: " + (errorCode ?? "not available"))
AuthorizationUIManager.logger.error(message: "errorDescription: " + (errorDescription ?? "not available"))
self.authorizationDelegate.onAuthorizationFailure(error: AuthorizationError.authorizationFailure("Failed to obtain access and identity tokens"))
}
})
return false
} else {
Expand All @@ -84,7 +90,7 @@ public class AuthorizationUIManager {

}




}
2 changes: 1 addition & 1 deletion Source/BluemixAppID/internal/OAuthManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Foundation
public class OAuthManager {
private(set) var appId:AppID
private(set) var preferenceManager:PreferenceManager
private(set) var registrationManager:RegistrationManager?
internal var registrationManager:RegistrationManager?
internal var authorizationManager:AuthorizationManager?
internal var tokenManager:TokenManager?

Expand Down
2 changes: 1 addition & 1 deletion Source/BluemixAppID/internal/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Utils {
if JSONSerialization.isValidJSONObject(value) {
do {
let data = try JSONSerialization.data(withJSONObject: value, options: options)
guard let string = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as? String else {
guard let string = String(data: data, encoding: String.Encoding.utf8) else {
throw AppIDError.jsonUtilsError(msg: "Json is malformed")
}
return string
Expand Down

0 comments on commit 25fe420

Please sign in to comment.