Plugin for the Kitura-Credentials framework that authenticates using a Dropbox OAuth2 token
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.
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.
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.
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()
}
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
...
}
This library is licensed under MIT. Full license text is available in LICENSE.