File: token.rb

package info (click to toggle)
ruby-doorkeeper 5.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 992 kB
  • sloc: ruby: 4,644; makefile: 4
file content (100 lines) | stat: -rw-r--r-- 3,080 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
59
60
61
62
63
64
65
66
67
68
69
70
71
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
# frozen_string_literal: true

module Doorkeeper
  module OAuth
    module Authorization
      class Token
        attr_reader :pre_auth, :resource_owner, :token

        class << self
          def build_context(pre_auth_or_oauth_client, grant_type, scopes, resource_owner)
            oauth_client = if pre_auth_or_oauth_client.respond_to?(:application)
                             pre_auth_or_oauth_client.application
                           elsif pre_auth_or_oauth_client.respond_to?(:client)
                             pre_auth_or_oauth_client.client
                           else
                             pre_auth_or_oauth_client
                           end

            Doorkeeper::OAuth::Authorization::Context.new(
              client: oauth_client,
              grant_type: grant_type,
              scopes: scopes,
              resource_owner: resource_owner,
            )
          end

          def access_token_expires_in(configuration, context)
            if configuration.option_defined?(:custom_access_token_expires_in)
              expiration = configuration.custom_access_token_expires_in.call(context)
              return nil if expiration == Float::INFINITY

              expiration || configuration.access_token_expires_in
            else
              configuration.access_token_expires_in
            end
          end

          def refresh_token_enabled?(server, context)
            if server.refresh_token_enabled?.respond_to?(:call)
              server.refresh_token_enabled?.call(context)
            else
              !!server.refresh_token_enabled?
            end
          end
        end

        def initialize(pre_auth, resource_owner)
          @pre_auth       = pre_auth
          @resource_owner = resource_owner
        end

        def issue_token!
          return @token if defined?(@token)

          context = self.class.build_context(
            pre_auth.client,
            Doorkeeper::OAuth::IMPLICIT,
            pre_auth.scopes,
            resource_owner,
          )

          @token = Doorkeeper.config.access_token_model.find_or_create_for(
            application: application,
            resource_owner: resource_owner,
            scopes: pre_auth.scopes,
            expires_in: self.class.access_token_expires_in(Doorkeeper.config, context),
            use_refresh_token: false,
          )
        end

        def application
          return unless pre_auth.client

          pre_auth.client.is_a?(Doorkeeper.config.application_model) ? pre_auth.client : pre_auth.client.application
        end

        def oob_redirect
          {
            controller: controller,
            action: :show,
            access_token: token.plaintext_token,
          }
        end

        def access_token?
          true
        end

        private

        def controller
          @controller ||= begin
            mapping = Doorkeeper::Rails::Routes.mapping[:token_info] || {}
            mapping[:controllers] || "doorkeeper/token_info"
          end
        end
      end
    end
  end
end