Class: SaeOauth2

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, access_token = nil, refresh_token = nil) ⇒ SaeOauth2

Returns a new instance of SaeOauth2.



13
14
15
16
17
18
19
20
21
# File 'lib/sae_oauth2.rb', line 13

def initialize(client_id, client_secret, access_token = nil, refresh_token = nil)
  @access_token_url = "https://api.weibo.com/oauth2/access_token"
  @authorize_url = "https://api.weibo.com/oauth2/authorize"

  @client_id = client_id
  @client_secret = client_secret
  @access_token = access_token
  @refresh_token = refresh_token
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



10
11
12
# File 'lib/sae_oauth2.rb', line 10

def access_token
  @access_token
end

#access_token_urlObject (readonly)

Returns the value of attribute access_token_url.



11
12
13
# File 'lib/sae_oauth2.rb', line 11

def access_token_url
  @access_token_url
end

#authorize_urlObject (readonly)

Returns the value of attribute authorize_url.



11
12
13
# File 'lib/sae_oauth2.rb', line 11

def authorize_url
  @authorize_url
end

#client_idObject

Returns the value of attribute client_id.



10
11
12
# File 'lib/sae_oauth2.rb', line 10

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



10
11
12
# File 'lib/sae_oauth2.rb', line 10

def client_secret
  @client_secret
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



10
11
12
# File 'lib/sae_oauth2.rb', line 10

def refresh_token
  @refresh_token
end

Instance Method Details

#get(url, parameters = {}) ⇒ Object



38
39
40
41
# File 'lib/sae_oauth2.rb', line 38

def get(url, parameters = {})
  response = self.oauth_request(url, 'GET', parameters)
  Yajl::Parser.parse(response)
end

#oauth_request(url, method, parameters) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/sae_oauth2.rb', line 48

def oauth_request(url, method, parameters)
  url = "#{@host}#{url}.json" unless url.starts_with?("https://")
  case method
  when 'GET'
    OpenSina.get(url, :query => parameters,  :headers => {"Authorization" => "OAuth2 #{@access_token}"}).body
  when 'POST'
    OpenSina.post(url, :query => parameters, :headers => {"Authorization" => "OAuth2 #{@access_token}"}).body
  end
end

#parse_signed_request(signed_request) ⇒ Object

Parameters:

  • string

    $signed_request 应用框架在加载iframe时会通过向Canvas URL post的参数signed_request



28
29
30
31
32
33
34
35
36
# File 'lib/sae_oauth2.rb', line 28

def parse_signed_request(signed_request)
  encoded_sig, payload = signed_request.split(".")
  sig = base64_url_decode(encoded_sig)
  data = Yajl::Parser.parse(base64_url_decode(payload))
  return nil if data["algorithm"].upcase != "HMAC-SHA256"

  expected_sig = OpenSSL::HMAC.digest("sha256", self.client_secret, payload)
  (sig != expected_sig) ? nil : data
end

#post(url, parameters = {}) ⇒ Object



43
44
45
46
# File 'lib/sae_oauth2.rb', line 43

def post(url, parameters = {})
  response = self.oauth_request(url, 'POST', parameters)
  Yajl::Parser.parse(response)
end