Class: OmniAuth::Strategies::Cloudfoundry

Inherits:
Object
  • Object
show all
Includes:
OmniAuth::Strategy
Defined in:
lib/omniauth/strategies/cloudfoundry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



32
33
34
# File 'lib/omniauth/strategies/cloudfoundry.rb', line 32

def access_token
  @access_token
end

#auth_server_urlObject (readonly)

Returns the value of attribute auth_server_url.



34
35
36
# File 'lib/omniauth/strategies/cloudfoundry.rb', line 34

def auth_server_url
  @auth_server_url
end

#token_issuerObject (readonly)

Returns the value of attribute token_issuer.



33
34
35
# File 'lib/omniauth/strategies/cloudfoundry.rb', line 33

def token_issuer
  @token_issuer
end

#token_server_urlObject (readonly)

Returns the value of attribute token_server_url.



35
36
37
# File 'lib/omniauth/strategies/cloudfoundry.rb', line 35

def token_server_url
  @token_server_url
end

Instance Method Details

#authorize_paramsObject



78
79
80
81
82
83
84
85
# File 'lib/omniauth/strategies/cloudfoundry.rb', line 78

def authorize_params
  params = options.authorize_params.merge(options.authorize_options.inject({}){|h,k| h[k.to_sym] = options[k] if options[k]; h})
  if OmniAuth.config.test_mode
    @env ||= {}
    @env['rack.session'] ||= {}
  end
  params
end

#build_access_token(query_string) ⇒ Object



136
137
138
139
# File 'lib/omniauth/strategies/cloudfoundry.rb', line 136

def build_access_token(query_string)
  log :info, "Fetching access token"
  client.authcode_grant(session.delete('redir_uri'), query_string)
end

#callback_phaseObject



91
92
93
94
95
96
97
98
# File 'lib/omniauth/strategies/cloudfoundry.rb', line 91

def callback_phase
  log :info, "In callback phase #{request.query_string}"
  self.access_token = build_access_token(request.query_string)
  self.access_token = refresh(access_token) if expired?(access_token)
  log :info, "Got access token #{access_token.inspect}"

  super
end

#callback_urlObject



67
68
69
# File 'lib/omniauth/strategies/cloudfoundry.rb', line 67

def callback_url
  full_host + script_name + callback_path
end

#clientObject



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
# File 'lib/omniauth/strategies/cloudfoundry.rb', line 38

def client

  unless @token_issuer
    unless @auth_server_url
      @auth_server_url ||= options.auth_server_url
      unless @auth_server_url.start_with?("http")
        @auth_server_url = "https://#{@auth_server_url}"
      end
    end

    unless @token_server_url
      @token_server_url = options.token_server_url || options.auth_server_url

      unless @token_server_url.start_with?("http")
        @token_server_url = "https://#{@token_server_url}"
      end
    end

    @token_issuer ||= CF::UAA::TokenIssuer.new(@auth_server_url,
                                               options.client_id,
                                               options.client_secret,
                                               {:token_target => @token_server_url})
    log :info, "Client: #{options.client_id} auth_server: #{@auth_server_url} token_server: #{@token_server_url}"
    @token_issuer.logger = OmniAuth.logger
  end

  @token_issuer
end

#expired?(access_token) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
150
# File 'lib/omniauth/strategies/cloudfoundry.rb', line 146

def expired?(access_token)
  access_token = access_token.auth_header if access_token.respond_to? :auth_header
  expiry = CF::UAA::TokenCoder.decode(access_token.split()[1], nil, nil, false)[:expires_at]		
  expiry.is_a?(Integer) && expiry <= Time.now.to_i
end

#prune!(hash) ⇒ Object



129
130
131
132
133
134
# File 'lib/omniauth/strategies/cloudfoundry.rb', line 129

def prune!(hash)
  hash.delete_if do |_, value|
    prune!(value) if value.is_a?(Hash)
    value.nil? || (value.respond_to?(:empty?) && value.empty?)
  end
end

#raw_infoObject



125
126
127
# File 'lib/omniauth/strategies/cloudfoundry.rb', line 125

def raw_info
  @raw_info ||= CF::UAA::Misc.whoami(@token_server_url, self.access_token.auth_header)
end

#refresh(access_token) ⇒ Object



141
142
143
144
# File 'lib/omniauth/strategies/cloudfoundry.rb', line 141

def refresh(access_token)
  log :info, "Refreshing access token"
  client.refresh_token_grant(access_token.info[:refresh_token])
end

#request_phaseObject



71
72
73
74
75
76
# File 'lib/omniauth/strategies/cloudfoundry.rb', line 71

def request_phase
  authcode_uri = client.authcode_uri(callback_url, options.scope)
  log :info, "Redirect URI #{authcode_uri}"
  session['redir_uri'] = authcode_uri
  redirect authcode_uri
end

#token_paramsObject



87
88
89
# File 'lib/omniauth/strategies/cloudfoundry.rb', line 87

def token_params
  options.token_params.merge(options.token_options.inject({}){|h,k| h[k.to_sym] = options[k] if options[k]; h})
end