DukeSwiftLibs 0.1.1

DukeSwiftLibs 0.1.1

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布上次发布2016年12月
SwiftSwift 版本3.0
SPM支持 SPM

Antti Tohmo 维护。



 
依赖
Alamofire~> 4.0
EVReflection>= 0
JWTDecode>= 0
Locksmith>= 0
SwiftyJSON>= 0
 

DukeSwiftLibs

概述

一套工具库,用于简化10Duke SSO和REST API功能在iOS移动开发中的使用。包含一个示例应用程序,展示了提供的API的使用示例。

示例

要运行示例 IdP SSO和REST API,先拷贝仓库,然后在示例目录中运行 pod install

示例应用程序将用户连接到正在运行的IdP服务实例。

示例应用程序功能

  • SSO 登录
  • SSO 登出
  • 登录用户页面(可修改)
  • 用户列表、查看、更新、创建和删除
  • 角色列表、查看、更新、创建和删除
  • 分组列表、查看、更新、创建和删除
  • 组织列表、查看、更新、创建和删除

客户端/API配置

Duke API 配置

配置可以在启动时进行一次,例如在 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")
  • 在示例应用程序中,ClientConfig 类提供了配置的值。
  • 网关端的SSO客户端ID和重定向URL值需要相应配置。

SSO 登录和登出的示例代码

SSO 登录和登出

// 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()

ApiToken 和 Bearer Token 使用

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)
//
  • DukeSwiftLibs中包含的WKWebViewController.swift类是一个使用上述令牌作为示例的WebView的工作示例。
  • RestApiImpl.swift类展示了一个示例实现,说明如何将Alamofire的cookies设置为包含bearer令牌。
  • SSOImpl.swift类中的进程池提供了一个机制,用于在WebView之间共享cookies。

Example REST API usage

User object CRUD operations examples

以下是样本代码的列表,与在提供的示例应用程序中使用的REST API相似的代码可以在此找到。

REST API example: Create User

// 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.")
    }
}

REST API example: Read User

如果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.")
        }
    }
}

REST API example: Update User

// 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.")
    }
}

REST API example: Delete User

// 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.")
    }
}

Requirements

Swift 3的实现的平台目标为iOS 10.0。

以下CocoaPods用于库实现内部依赖。

Installation

DukeSwiftLibs通过CocoaPods提供。要安装它,只需将以下行添加到您的Podfile

pod "DukeSwiftLibs"

使用以下命令安装pods后

pod install

您可以在.swift文件中将DukeSwiftLibs作为导入使用

import DukeSwiftLibs

Author

Antti Tohmo,[email protected]

License

DukeSwiftLibs在MIT许可下可用。有关更多信息,请参阅LICENSE文件。