Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add proxy login.

  • Loading branch information...
commit 91ac1734774aed3cd5b11c8a5e0f179aa5047670 1 parent 833f6c2
@robey authored
Showing with 18 additions and 13 deletions.
  1. +15 −6 src/fauna/fauna_client.coffee
  2. +3 −7 test/test_fauna_client.coffee
View
21 src/fauna/fauna_client.coffee
@@ -32,7 +32,9 @@ requireTokenOrPublisher = (f) ->
(x...) ->
if not (@publisherKey? or @userToken?)
return Q.reject(new FaunaError(error: "Requires authentication as publisher or user"))
- if @userToken?
+ if @proxyLogin? and @publisherKey?
+ @setAuth @publisherKey, @proxyLogin
+ else if @userToken?
@setAuth @userToken, ""
else
@setAuth @publisherKey, ""
@@ -79,6 +81,7 @@ class FaunaClient
@publisherKey = null
@clientKey = null
@userToken = null
+ @proxyLogin = null
# actual auth string sent to server
@auth = ""
# all js/json object transformations use the schema:
@@ -138,6 +141,16 @@ class FaunaClient
###
@userToken = key
+ setProxyLogin: (userId) ->
+ ###
+ Set a proxy-login user. If a token is can be used as authentication for
+ a command, and both a proxy login and publisher key are set, then the
+ publisher key will be used to force authentication as the requested user.
+ This allows a publisher to perform user-level commands "on behalf of" a
+ user, without knowing the user's password or token.
+ ###
+ @proxyLogin = userId
+
deleteEverything: requireOwner asObject -> @rest("delete", "everything")
installSchema: ->
@@ -169,10 +182,6 @@ class FaunaClient
login: requireClientOrPublisher asObject (email, password) ->
@rest("post", "tokens", email: email, password: password)
- # the publisher may login as another user, using the publisher key as auth.
- loginAsUser: requirePublisher (email) ->
- @login(@publisherKey, email)
-
class _PublisherKeys
get: requireOwner asEventArray -> @rest("get", "keys/publisher")
create: requireOwner asObject -> @rest("post", "keys/publisher")
@@ -239,7 +248,7 @@ class FaunaClient
"#{@protocol}://#{@auth}@#{@hostname}/#{@apiVersion}/#{path}"
setAuth: (username, password) ->
- @auth = "#{encodeURIComponent(username)}:#{escape(password)}"
+ @auth = "#{encodeURIComponent(username)}:#{encodeURIComponent(password)}"
@debug "Auth as #{@auth}"
rest: (method, path, data) ->
View
10 test/test_fauna_client.coffee
@@ -174,13 +174,9 @@ describe "FaunaClient", ->
it "can login as a user", futureTest ->
f = new fauna.FaunaClient()
f.setPublisherKey("winner")
- r = -> f.loginAsUser("test@example.com")
- withSuccessfulRequest(data4, r).then ([ resp, requests ]) ->
- requests.length.should.equal(1)
- requests[0].method.should.eql("POST")
- requests[0].url.should.match(/\/tokens/)
- requests[0].body.should.match(/"email":"winner"/)
- resp.token.should.eql("AQIAayWqL0AAAQBrJan6kAABAGslqZywAAEBAGslqfewAAHrRxQyyxxJ_FgqRBbkmjVjAAAAAAAAAA")
+ f.setProxyLogin("test@example.com")
+ f._testing.requireTokenOrPublisher("test").then ->
+ f.auth.should.eql("winner:test@example.com")
# test data from the fauna documentation for GET keys/publisher
data1 = """
Please sign in to comment.
Something went wrong with that request. Please try again.