测试已测试 | ✗ |
Lang语言 | SwiftSwift |
许可证 | MIT |
发布上次发布 | 2016年12月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Antti Tohmo 维护。
依赖 | |
Alamofire | ~> 4.0 |
EVReflection | >= 0 |
JWTDecode | >= 0 |
Locksmith | >= 0 |
SwiftyJSON | >= 0 |
一套工具库,用于简化10Duke SSO和REST API功能在iOS移动开发中的使用。包含一个示例应用程序,展示了提供的API的使用示例。
要运行示例 IdP SSO和REST API,先拷贝仓库,然后在示例目录中运行 pod install
。
示例应用程序将用户连接到正在运行的IdP服务实例。
配置可以在启动时进行一次,例如在 AppDelegate 类中。
// 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!)
}
成功登录会存储 OAuth2 Bearer token 到 ApiTokenImpl 单例中。参见示例应用程序中的参考实现。
// Get the singleton of ApiToken
let apiToken = ApiTokenImpl.shared
//
let userId = apiToken.getUserId()
//
let token = apiToken.getToken()
Oauth2 Bearer token 可用于 HTTP 请求,例如在 REST API 的认证头中。
//
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)
//
以下是样本代码的列表,与在提供的示例应用程序中使用的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.")
}
}
如果API返回用户,则将10Duke storyboard的UserViewController推送到UI中。
// 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.")
}
}
Swift 3的实现的平台目标为iOS 10.0。
以下CocoaPods用于库实现内部依赖。
DukeSwiftLibs通过CocoaPods提供。要安装它,只需将以下行添加到您的Podfile
pod "DukeSwiftLibs"
使用以下命令安装pods后
pod install
您可以在.swift文件中将DukeSwiftLibs作为导入使用
import DukeSwiftLibs
Antti Tohmo,[email protected]
DukeSwiftLibs在MIT许可下可用。有关更多信息,请参阅LICENSE文件。