CredentialsDropbox

master

Kitura Credentials framework OAuth2 token type authorization validation for Dropbox
crspybits/CredentialsDropbox

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.

Description

  • Swift Tools 5.0.0
View More Packages from this Author

Dependencies

Last updated: Mon Mar 18 2024 17:14:55 GMT-0900 (Hawaii-Aleutian Daylight Time)