Class: Masks::OpenID::Authorization

Inherits:
Object
  • Object
show all
Defined in:
app/models/masks/openid/authorization.rb

Overview

Manages authorizations for OpenID/OAuth2 requests.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, **opts) ⇒ Authorization

Returns a new instance of Authorization.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/masks/openid/authorization.rb', line 16

def initialize(env, **opts)
  @env = env
  @app =
    Rack::OAuth2::Server::Authorize.new do |req, res|
      @client =
        session.config.model(:openid_client).find_by(key: req.client_id)

      req.bad_request!(:client_id, "not found") unless @client

      unless req.redirect_uri
        req.invalid_request!('"redirect_uri" missing')
      end

      unless @client.redirect_uris.any?
        @client.redirect_uris = [req.redirect_uri.to_s]
        @client.valid? || req.invalid_request!('"redirect_uri" invalid')
      end

      res.redirect_uri = req.verify_redirect_uri!(@client.redirect_uris)

      @scopes = req.scope & @client.scopes

      if res.protocol_params_location == :fragment && req.nonce.blank?
        req.invalid_request! "nonce required"
      end

      if @client.response_types.include?(
           Array(req.response_type).collect(&:to_s).join(" ")
         )
        if actor
          if opts[:approved] || client.auto_consent?
            @client.save if @client.redirect_uris_changed?

            approved! req, res
          elsif opts.key?(:approved)
            req.access_denied!
          end
        end
      else
        req.unsupported_response_type!
      end
    end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



6
7
8
# File 'app/models/masks/openid/authorization.rb', line 6

def client
  @client
end

#responseObject

Returns the value of attribute response.



6
7
8
# File 'app/models/masks/openid/authorization.rb', line 6

def response
  @response
end

#response_typeObject

Returns the value of attribute response_type.



6
7
8
# File 'app/models/masks/openid/authorization.rb', line 6

def response_type
  @response_type
end

#scopesObject

Returns the value of attribute scopes.



6
7
8
# File 'app/models/masks/openid/authorization.rb', line 6

def scopes
  @scopes
end

Class Method Details

.perform(env, **opts) ⇒ Object



9
10
11
12
13
# File 'app/models/masks/openid/authorization.rb', line 9

def perform(env, **opts)
  authorization = new(env, **opts)
  authorization.perform
  authorization
end

Instance Method Details

#actorObject



64
65
66
# File 'app/models/masks/openid/authorization.rb', line 64

def actor
  @actor ||= (session.actor if session.passed?)
end

#approved!(req, res) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/masks/openid/authorization.rb', line 72

def approved!(req, res)
  response_types = Array(req.response_type)

  if response_types.include? :code
    authorization =
      actor.openid_authorizations.create!(
        openid_client: client,
        redirect_uri: res.redirect_uri,
        nonce: req.nonce,
        scopes: @scopes
      )

    res.code = authorization.code
  end

  if response_types.include? :token
    access_token =
      actor.openid_access_tokens.create!(
        openid_client: client,
        scopes: @scopes
      )

    res.access_token = access_token.to_bearer_token
  end

  if response_types.include? :id_token
    id_token =
      actor.openid_id_tokens.create!(
        openid_client: @client,
        nonce: req.nonce
      )

    res.id_token =
      id_token.to_jwt(
        code: (res.respond_to?(:code) ? res.code : nil),
        access_token:
          (res.respond_to?(:access_token) ? res.access_token : nil)
      )
  end

  res.approve!
end

#performObject



68
69
70
# File 'app/models/masks/openid/authorization.rb', line 68

def perform
  @response = @app.call(@env)
end

#sessionObject



60
61
62
# File 'app/models/masks/openid/authorization.rb', line 60

def session
  @session ||= @env[Masks::Middleware::SESSION_KEY]
end