rack-oauth2-provider 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rack/oauth2/assertion_profile.rb +25 -3
- data/lib/rack/oauth2/provider.rb +2 -1
- data/lib/simple_web_token_builder.rb +20 -1
- data/rakefile +2 -1
- metadata +3 -3
@@ -6,12 +6,24 @@ require 'vendor/information_card'
|
|
6
6
|
|
7
7
|
module Rack
|
8
8
|
module OAuth2
|
9
|
+
# Rack::OAuth2::AssertionProfile implements the Assertion Profile for generating
|
10
|
+
# authorization tokens as per draft-ieft-oauth. This is a preliminary version based on the
|
11
|
+
# Apr 16, 2010 working standard developed by the IETF.
|
12
|
+
#
|
13
|
+
# Initialize with the Rack application that will work as Authorization Server,
|
14
|
+
# and a set of parameters that enables specific checks. The only mandatory parameter
|
15
|
+
# is **:shared_secret** which is required for HMAC-SHA256 processing.
|
9
16
|
class AssertionProfile < Rack::Auth::AbstractHandler
|
17
|
+
|
18
|
+
# Creates a new instance of Rack::OAuth2::Provider, the opts are required
|
10
19
|
def initialize(app, opts = {})
|
11
20
|
@app = app
|
12
21
|
@opts = opts
|
13
22
|
end
|
14
|
-
|
23
|
+
|
24
|
+
# Authorizes the request and generates the _access token_ on the body,
|
25
|
+
# signed with the shared key (passed as c'tor parameter),
|
26
|
+
# as a successful response of the token processing.
|
15
27
|
def call(env)
|
16
28
|
request = Request.new(env)
|
17
29
|
|
@@ -30,24 +42,34 @@ module Rack
|
|
30
42
|
|
31
43
|
return @app.call(env)
|
32
44
|
end
|
33
|
-
|
45
|
+
|
46
|
+
# Singleton instance of the SimpleWebTokenBuilder
|
47
|
+
#
|
48
|
+
# see alse: SimpleWebToken::SimpleWebTokenBuilder
|
34
49
|
def token_builder
|
35
50
|
@token_builder ||= SimpleWebToken::SimpleWebTokenBuilder.new(@opts)
|
36
51
|
end
|
37
|
-
|
52
|
+
|
53
|
+
# Internal class used to parse the current request based on
|
54
|
+
# the enviroment parameters.
|
38
55
|
class Request < Rack::Request
|
39
56
|
def initialize(env)
|
40
57
|
super(env)
|
41
58
|
end
|
42
59
|
|
60
|
+
# Returns a value indicating whether the type
|
61
|
+
# the of authorization request is _assertion_
|
43
62
|
def assertion_profile?
|
44
63
|
self.params["type"] =~ /assertion/i
|
45
64
|
end
|
46
65
|
|
66
|
+
# Reads from the formvars the format of the
|
67
|
+
# set assertion
|
47
68
|
def format
|
48
69
|
(self.params["format"] or "saml").downcase.to_sym
|
49
70
|
end
|
50
71
|
|
72
|
+
# Reads the assertion from the given formvars
|
51
73
|
def token
|
52
74
|
self.params["assertion"]
|
53
75
|
end
|
data/lib/rack/oauth2/provider.rb
CHANGED
@@ -3,9 +3,13 @@ require 'base64'
|
|
3
3
|
require 'hmac-sha2'
|
4
4
|
|
5
5
|
module SimpleWebToken
|
6
|
+
# Creates a SimpleWebToken using the given parameters
|
7
|
+
# plus a hash containing "claims"
|
6
8
|
class SimpleWebTokenBuilder
|
7
9
|
attr_accessor :shared_secret, :issuer, :audience, :expiration
|
8
10
|
|
11
|
+
# Creates a new instance of the SimpleTokenBuilder,
|
12
|
+
# if <b>:shared_secret</b> is not provided, an exception will be raised
|
9
13
|
def initialize(opts = {})
|
10
14
|
raise InvalidOption, :shared_secret unless opts[:shared_secret]
|
11
15
|
self.shared_secret = opts[:shared_secret]
|
@@ -14,19 +18,25 @@ module SimpleWebToken
|
|
14
18
|
self.expiration = (opts[:expiration] or 3600)
|
15
19
|
end
|
16
20
|
|
21
|
+
# Creates and signs the token based on the given claims hash
|
22
|
+
# plus the default claims set (issuer, audience, expires_on)
|
17
23
|
def build(claims)
|
18
24
|
token = (convert(claims) + default_claim_set).join("&")
|
19
25
|
return token += "&HMACSHA256=#{CGI.escape(sign(token))}"
|
20
26
|
end
|
21
|
-
|
27
|
+
|
28
|
+
# Creates the HMAC-SHA256 signature based on the form-enconded-values
|
29
|
+
# representation of the token
|
22
30
|
def sign(bare_token)
|
23
31
|
signature = Base64.encode64(HMAC::SHA256.new(Base64.decode64(self.shared_secret)).update(bare_token.toutf8).digest).strip
|
24
32
|
end
|
25
33
|
|
34
|
+
# Converts a hash of claims into a claim-value pair
|
26
35
|
def convert(claims)
|
27
36
|
claims.map{|k, v| claim_pair(k, v)}
|
28
37
|
end
|
29
38
|
|
39
|
+
# Returns the default claim set (issuer, audience, expires_on)
|
30
40
|
def default_claim_set
|
31
41
|
default_claims = []
|
32
42
|
default_claims << claim_pair(:issuer, self.issuer) if(self.issuer)
|
@@ -35,6 +45,15 @@ module SimpleWebToken
|
|
35
45
|
return default_claims
|
36
46
|
end
|
37
47
|
|
48
|
+
# Creates a claim-value pair
|
49
|
+
#
|
50
|
+
# The given key is converted to PascalCase and merged (_ are removed,
|
51
|
+
# words between _ are considered discrete terms hence are uppercased)
|
52
|
+
#
|
53
|
+
# Values and Keys are encoded using CGI urlEscaping
|
54
|
+
#
|
55
|
+
# NOTE: If the claim value is an array, the given claim value is built
|
56
|
+
# as csv (comma-separted-values)
|
38
57
|
def claim_pair(key, value)
|
39
58
|
new_key = key.to_s.downcase.split("_").map{|l| l.capitalize.strip}.join("")
|
40
59
|
value = [value].flatten.uniq.join(",")
|
data/rakefile
CHANGED
@@ -21,7 +21,8 @@ namespace :docs do
|
|
21
21
|
t.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
|
22
22
|
t.options << '--charset' << 'utf-8'
|
23
23
|
t.rdoc_files.include('README.rdoc')
|
24
|
-
t.rdoc_files.include('lib
|
24
|
+
t.rdoc_files.include('lib/*.rb')
|
25
|
+
t.rdoc_files.include('lib/rack/**/*.rb')
|
25
26
|
end
|
26
27
|
end
|
27
28
|
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Johnny G. Halife & Ezequiel Morito
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-04-
|
17
|
+
date: 2010-04-21 00:00:00 -03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|