TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Dec 2016 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by Antti Tohmo.
Depends on: | |
Alamofire | ~> 4.0 |
EVReflection | >= 0 |
JWTDecode | >= 0 |
Locksmith | >= 0 |
SwiftyJSON | >= 0 |
A library set of tools to ease the iOS mobile development of the 10Duke SSO and REST API functionality. An example application is included that illustrates the use of the provided APIs.
To run the example IdP SSO and REST API using project, clone the repo, and run pod install
from the Example directory first.
The example application connects the user to a running instance of the IdP service.
The configuration can be done once i.e. within the AppDelegate class during app startup.
// Get the singleton instance of the ApiConfig
let apiConfig: ApiConfigImpl = ApiConfigImpl.shared
apiConfig.setIdPBaseUrl("http://vslidp.10duke.com/")
apiConfig.setSSOClientId("ios_test")
apiConfig.setSSORedirectUrl("tendukeauthapp://oauth/callback")
// Get the singleton of the SSO
let sso = SSOImpl.shared
//
// Check if user is currently logged in to provide right action
if let sso.isUserLoggedIn() {
// Logout
sso.logout(controller: self.navigationController!)
} else {
// Login
sso.login(controller: self.navigationController!)
}
A successful login will store the OAuth2 Bearer token to the ApiTokenImpl singleton. See the example application for a reference implementation.
// Get the singleton of ApiToken
let apiToken = ApiTokenImpl.shared
//
let userId = apiToken.getUserId()
//
let token = apiToken.getToken()
The Oauth2 Bearer token can be used in HTTP requests i.e. towards the REST API in the authentication haeder.
//
let webConfiguration = WKWebViewConfiguration()
webConfiguration.processPool = SSOImpl.shared.getProcessPool()
let webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.navigationDelegate = self
//
let token = apiToken.getToken()
let requestUrl = URL(string: "http://www.example.com/")
var request = URLRequest(url: requestUrl)
//
if token != nil {
//
request.setValue("Bearer \(m_token)", forHTTPHeaderField: "Authorization")
}
//
webView.load(request)
//
Here is a list of sample code, similar to which can be found in the provided sample app that demonstrates the use of the REST API.
// Get the singleton of the REST API
let restApi = RestApiImpl.shared
//
// User "CREATE" request
restApi.createUser(user, completion: createCallback(_:))
//
// User "CREATE" callback
func createCallback(_ success: Bool) {
//
if success {
//
m_createButton?.isEnabled = false
} else {
//
showError(title: "User create failed", message: "Press Ok to continue.")
}
}
Pushes the 10Duke storyboard UserViewController to the UI in case user is returned from the API.
// Get the singleton of the REST API
let restApi = RestApiImpl.shared
//
// Get the singleton of the api token
let apiToken = ApiTokenImpl.shared
//
if let userId = apiToken.getUserId() {
//
restApi.getUser(id: id, completion: getUserCallback(_:))
//
// User "READ" callback
func userCallback(_ user: User?) {
//
if let myUser = user {
//
let storyboard = UIStoryboard(name: "10Duke", bundle: nil)
let userViewController = storyboard.instantiateViewController(withIdentifier: "UserViewController") as! UserViewController
userViewController.m_user = myUser
self.navigationController?.pushViewController(userViewController, animated: true)
} else {
//
showError(title: "User read failed", message: "Press Ok to continue.")
}
}
}
// Get the singleton of the REST API
let restApi = RestApiImpl.shared
//
let user = <your-user-object-here>
//
// User "UPDATE" request
restApi.updateUser(user, completion: updateCallback(_:))
//
// User "UPDATE" callback
func updateCallback(_ success: Bool) {
//
if success {
//
// Disables update action clickable button visible on screen.
m_updateButton?.isEnabled = false
} else {
//
showError(title: "User update failed", message: "Press Ok to continue.")
}
}
// Get the singleton of the REST API
let restApi = RestApiImpl.shared
//
let user = <your-user-object-here>
//
// User "DELETE" request
restApi.deleteUser(user, completion: deleteCallback(_:))
//
// User "DELETE" callback
func deleteCallback(_ success: Bool) {
//
if success {
//
// Disables delete action clickable button visible on screen.
m_updateButton?.isEnabled = false
} else {
//
showError(title: "User delete failed", message: "Press Ok to continue.")
}
}
The Swift 3 implementation has a platform target iOS 10.0.
The following CocoaPods are used as internal dependencies in the library implementation:
DukeSwiftLibs is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "DukeSwiftLibs"
After installing the pod using:
pod install
You can use the DukeSwiftLibs as an import in your .swift files:
import DukeSwiftLibs
Antti Tohmo, [email protected]
DukeSwiftLibs is available under the MIT license. See the LICENSE file for more info.