Class: Trello::Authorization::OAuthPolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/trello/authorization.rb

Overview

Handles the OAuth connectivity to Trello.

For 2-legged OAuth, do the following:

OAuthPolicy.consumer_credential = OAuthCredential.new "public_key", "secret"
OAuthPolicy.token               = OAuthCredential.new "token_key", nil

For 3-legged OAuth, do the following:

OAuthPolicy.consumer_credential = OAuthCredential.new "public_key", "secret"
OAuthPolicy.return_url          = "http://your.site.com/path/to/receive/post"
OAuthPolicy.callback            = Proc.new do |request_token|
  DB.save(request_token.key, request_token.secret)
  redirect_to request_token.authorize_url
end

Then, recreate the request token given the request token key and secret you saved earlier, and the consumer, and pass that RequestToken instance the #get_access_token method, and store that in OAuthPolicy.token as a OAuthCredential.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ OAuthPolicy

Returns a new instance of OAuthPolicy.



84
85
86
87
88
89
90
91
# File 'lib/trello/authorization.rb', line 84

def initialize(attrs = {})
  @consumer_key       = attrs[:consumer_key]
  @consumer_secret    = attrs[:consumer_secret]
  @oauth_token        = attrs[:oauth_token]
  @oauth_token_secret = attrs[:oauth_token_secret]
  @return_url         = attrs[:return_url]          || self.class.return_url
  @callback           = attrs[:callback]            || self.class.callback
end

Class Attribute Details

.callbackObject

Returns the value of attribute callback.



74
75
76
# File 'lib/trello/authorization.rb', line 74

def callback
  @callback
end

.consumer_credentialObject

Returns the value of attribute consumer_credential.



74
75
76
# File 'lib/trello/authorization.rb', line 74

def consumer_credential
  @consumer_credential
end

.return_urlObject

Returns the value of attribute return_url.



74
75
76
# File 'lib/trello/authorization.rb', line 74

def return_url
  @return_url
end

.tokenObject

Returns the value of attribute token.



74
75
76
# File 'lib/trello/authorization.rb', line 74

def token
  @token
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



81
82
83
# File 'lib/trello/authorization.rb', line 81

def attributes
  @attributes
end

#callbackObject

Returns the value of attribute callback.



82
83
84
# File 'lib/trello/authorization.rb', line 82

def callback
  @callback
end

#consumer_credentialObject

Returns the value of attribute consumer_credential.



82
83
84
# File 'lib/trello/authorization.rb', line 82

def consumer_credential
  @consumer_credential
end

#return_urlObject

Returns the value of attribute return_url.



82
83
84
# File 'lib/trello/authorization.rb', line 82

def return_url
  @return_url
end

#tokenObject

Returns the value of attribute token.



82
83
84
# File 'lib/trello/authorization.rb', line 82

def token
  @token
end

Class Method Details

.authorize(request) ⇒ Object



76
77
78
# File 'lib/trello/authorization.rb', line 76

def authorize(request)
  new.authorize(request)
end

Instance Method Details

#authorize(request) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/trello/authorization.rb', line 93

def authorize(request)
  unless consumer_credential
    Trello.logger.error "The consumer_credential has not been supplied."
    fail "The consumer_credential has not been supplied."
  end

  if token
    request.headers = {"Authorization" => get_auth_header(request.uri, :get)}
    request
  else
    consumer(return_url: return_url, callback_method: :postMessage)
    request_token = consumer.get_request_token(oauth_callback: return_url)
    callback.call request_token
    return nil
  end
end

#consumer_keyObject



118
119
120
# File 'lib/trello/authorization.rb', line 118

def consumer_key
  consumer_credential.key
end

#consumer_secretObject



122
123
124
# File 'lib/trello/authorization.rb', line 122

def consumer_secret
  consumer_credential.secret
end

#oauth_tokenObject



126
127
128
# File 'lib/trello/authorization.rb', line 126

def oauth_token
  token.key
end

#oauth_token_secretObject



130
131
132
# File 'lib/trello/authorization.rb', line 130

def oauth_token_secret
  token.secret
end