目录
目录README.md

CredentialsDropbox

Plugin for the Kitura-Credentials framework that authenticates using a Dropbox OAuth2 token

Summary

Plugin for Kitura-Credentials framework that authenticates using a Dropbox OAuth2 token that was acquired by a mobile app or other client of the Kitura based backend.

Table of Contents

Swift version

The latest version of CredentialsDropbox requires Swift 4.0. You can download this version of the Swift binaries by following this link. Compatibility with other Swift versions is not guaranteed.

Example of authentication with a Dropbox OAuth2 token

This example shows how to use CredentialsDropboxToken plugin to authenticate post requests, it shows both the server side and the client side of the request involved.

Server side

First create an instance of Credentials and an instance of CredentialsDropboxToken plugin:

import Credentials
import CredentialsDropbox

let credentials = Credentials()
let dropboxCredentials = CredentialsDropboxToken(options: options)

Where:

  • options is an optional dictionary ([String:Any]) of Dropbox authentication options whose keys are listed in CredentialsDropboxOptions.

Now register the plugin:

credentials.register(dropboxCredentials)

Connect credentials middleware to post requests:

router.post("/collection/:new", middleware: credentials)

If the authentication is successful, request.userProfile will contain user profile information received from Dropbox:

router.post("/collection/:new") {request, response, next in
  ...
  let profile = request.userProfile
  let userId = profile.id
  let userName = profile.displayName
  ...
  next()
}

Client side

The client needs to put a Dropbox access token in the request’s access_token HTTP header field, and “DropboxToken” in the X-token-type field. And because Dropbox apparently doesn’t have a direct means to validate an access token, you need to pass the dropbox uid with the header key X-account-id:

let urlRequest = NSMutableURLRequest(URL: NSURL(string: "http://\(serverUrl)/collection/\(name)"))
urlRequest.HTTPMethod = "POST"
urlRequest.HTTPBody = ...

urlRequest.addValue(dropboxAccessToken, forHTTPHeaderField: "access_token")
urlRequest.addValue("DropboxToken", forHTTPHeaderField: "X-token-type")
urlRequest.addValue(dropboxUid, forHTTPHeaderField: "X-account-id")
Alamofire.request(urlRequest).responseJSON {response in
  ...
}

License

This library is licensed under MIT. Full license text is available in LICENSE.

关于
53.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号