firebase_token_generator 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/firebase_token_generator.rb +82 -0
  2. metadata +46 -0
@@ -0,0 +1,82 @@
1
+ require "json"
2
+ require "base64"
3
+ require "openssl"
4
+
5
+ module Firebase
6
+
7
+ # This class handles generating signed authentication tokens for use with Firebase
8
+ class FirebaseTokenGenerator
9
+
10
+ # When creating an instance of the generator, you must provide your Firebase Application Secret
11
+ def initialize(secret)
12
+ @secret = secret
13
+ end
14
+
15
+ # Returns a signed Firebase Authentication Token
16
+ # Takes the following arguments:
17
+ # [auth_data] A hash of arbitrary data to be included in the token
18
+ # [options] An optional hash of extra claims that may be included in the token. Allowed values are:
19
+ # [expires] Epoch time after which the token will no longer be valid
20
+ # [notBefore] Epoch time before which the token will not be valid
21
+ # [admin] If set to true, this client will bypass all security rules
22
+ # [debug] If set to true, this client will receive debug information about the security rules
23
+ # [simulate] (internal-only for now) Runs security rules but makes no data changes
24
+ #
25
+ # Throws ArgumentError if given an invalid option
26
+ def create_token(auth_data, options = {})
27
+ claims = create_options_claims(options)
28
+ claims[:v] = TOKEN_VERSION
29
+ claims[:iat] = Time.now.to_i
30
+ claims[:d] = auth_data
31
+ encode_token(claims)
32
+ end
33
+
34
+ private
35
+
36
+ TOKEN_VERSION = 0
37
+
38
+ TOKEN_SEP = "."
39
+
40
+ CLAIMS_MAP = {
41
+ :expires => :exp,
42
+ :notBefore => :nbf,
43
+ :admin => :admin,
44
+ :debug => :debug,
45
+ :simulate => :simulate
46
+ }
47
+
48
+ def create_options_claims(options)
49
+ opts = {}
50
+ options.each do |key, value|
51
+ if CLAIMS_MAP.include?(key.to_sym) then
52
+ opts[key.to_sym] = value
53
+ else
54
+ raise ArgumentError, "#{key.to_s} is not a valid option"
55
+ end
56
+ end
57
+ opts
58
+ end
59
+
60
+ def encode_token(claims)
61
+ encoded_header = encode_json({:typ => "JWT", :alg => "HS256"})
62
+ encoded_claims = encode_json(claims)
63
+ secure_bits = [encoded_header, encoded_claims].join(TOKEN_SEP)
64
+ sig = sign(secure_bits)
65
+ [encoded_header, encoded_claims, sig].join(TOKEN_SEP)
66
+ end
67
+
68
+ def encode_json(obj)
69
+ encode(JSON.dump(obj))
70
+ end
71
+
72
+ def encode(s)
73
+ Base64.urlsafe_encode64(s).gsub('=', '')
74
+ end
75
+
76
+ def sign(to_sign)
77
+ encode(OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, @secret, to_sign))
78
+ end
79
+
80
+ end
81
+
82
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: firebase_token_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Greg Soltis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-24 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A library for generating signed authentication tokens for use with Firebase.
15
+ Uses your app secret.
16
+ email: greg@firebase.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/firebase_token_generator.rb
22
+ homepage: http://github.com/firebase/FirebaseTokenGenerator-ruby
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.24
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Generate Firebase Authentication Tokens
46
+ test_files: []