class RHC::Auth::Token

Attributes

auth[R]
options[R]
store[R]
token[R]

Public Class Methods

new(opt, auth=nil, store=nil) click to toggle source
# File lib/rhc/auth/token.rb, line 3
def initialize(opt, auth=nil, store=nil)
  if opt.is_a?(String)
    @token = opt
  else
    @options = opt || Commander::Command::Options.new
    @token = options[:token]
    @no_interactive = options[:noprompt]
    @allows_tokens = options[:use_authorization_tokens]
  end
  @auth = auth
  @store = store
  read_token
end

Public Instance Methods

can_authenticate?() click to toggle source
# File lib/rhc/auth/token.rb, line 43
def can_authenticate?
  token || auth && auth.can_authenticate?
end
retry_auth?(response, client) click to toggle source
# File lib/rhc/auth/token.rb, line 26
def retry_auth?(response, client)
  if response.status == 401
    token_rejected(response, client)
  else
    false
  end
end
save(token) click to toggle source
# File lib/rhc/auth/token.rb, line 38
def save(token)
  store.put(username, openshift_server, token) if store
  @token = token
end
to_request(request) click to toggle source
# File lib/rhc/auth/token.rb, line 17
def to_request(request)
  if token
    (request[:headers] ||= {})['authorization'] = "Bearer #{token}"
  elsif auth and (!@allows_tokens or @can_get_token == false)
    auth.to_request(request)
  end
  request
end
username() click to toggle source
# File lib/rhc/auth/token.rb, line 34
def username
  auth && auth.respond_to?(:username) && auth.username || options[:username]
end

Protected Instance Methods

cannot_retry?() click to toggle source
# File lib/rhc/auth/token.rb, line 94
def cannot_retry?
  !@fetch_once && @no_interactive
end
read_token() click to toggle source
# File lib/rhc/auth/token.rb, line 90
def read_token
  @token ||= store.get(username, openshift_server) if store
end
token_rejected(response, client) click to toggle source
# File lib/rhc/auth/token.rb, line 51
def token_rejected(response, client)
  has_token = !!token
  @token = nil

  unless auth && auth.can_authenticate?
    if has_token
      raise RHC::Rest::TokenExpiredOrInvalid, "Your authorization token is expired or invalid."
    end
    return false
  end

  if has_token
    if cannot_retry?
      raise RHC::Rest::TokenExpiredOrInvalid, "Your authorization token is expired or invalid."
    end
    if not client.supports_sessions?
      raise RHC::Rest::AuthorizationsNotSupported
    end
  end

  @can_get_token = client.supports_sessions? && @allows_tokens

  if has_token
    warn "Your authorization token has expired. Please sign in now to continue."
  elsif @can_get_token
    info "Please sign in to start a new session to #{openshift_server}."
  end

  return auth.retry_auth?(response, client) unless @can_get_token

  if auth_token = client.new_session(:auth => auth)
    @fetch_once = true
    save(auth_token.token)
    true
  else
    auth.retry_auth?(response, client)
  end
end