Skip to content

Commit

Permalink
Added token_for method
Browse files Browse the repository at this point in the history
  • Loading branch information
arrowcircle committed Feb 3, 2015
1 parent 45eefdc commit 125a4d1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ Gets message history of the channel:

client.history('test_channel')

### JS Client token generation

Generates token for JS client:

client.token_for('testuser', '123123')

Where `123123` is UNIX timestamp. You can also add user info as valid json string as third parameter:

client.token_for('testuser', '123123', "{}")

### Other API

Other API methods, like projects and channels management are unavailable now.
Expand Down
15 changes: 1 addition & 14 deletions lib/centrifuge/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(method, data, client)

def process
body = { data: json(method, data) }
body.merge!(sign: sign(body[:data]))
body.merge!(sign: client.sign(body[:data]))
Centrifuge::Request.new(client.client, 'POST', client.url, nil, body).send
end

Expand All @@ -19,18 +19,5 @@ def process
def json(method, params)
MultiJson.dump({ method: method, params: params })
end

def sign(body)
dig = OpenSSL::Digest.new('md5')
OpenSSL::HMAC.hexdigest(dig, secret, "#{project_id}#{body}")
end

def project_id
client.project_id
end

def secret
client.secret
end
end
end
9 changes: 9 additions & 0 deletions lib/centrifuge/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def history(channel)
Centrifuge::Builder.new('history', { channel: channel }, self).process
end

def token_for(user, timestamp, user_info = "")
sign("#{user}#{timestamp}#{user_info}")
end

def sign(body)
dig = OpenSSL::Digest.new('md5')
OpenSSL::HMAC.hexdigest(dig, secret, "#{project_id}#{body}")
end

def client
@client ||= begin
HTTPClient.new.tap do |http|
Expand Down
2 changes: 1 addition & 1 deletion lib/centrifuge/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Centrifuge
VERSION = "0.0.1"
VERSION = "0.0.2"
end

0 comments on commit 125a4d1

Please sign in to comment.