Class: GData::GoogleAuthSub
- Inherits:
-
Object
- Object
- GData::GoogleAuthSub
- Defined in:
- lib/googleauthsub.rb
Overview
GoogleAuthSub This class handles the Google Authentication for Web Applications API
Instance Attribute Summary collapse
-
#next_url ⇒ Object
Returns the value of attribute next_url.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#secure ⇒ Object
Returns the value of attribute secure.
-
#session ⇒ Object
Returns the value of attribute session.
-
#sigalg ⇒ Object
Returns the value of attribute sigalg.
-
#target ⇒ Object
Returns the value of attribute target.
-
#token ⇒ Object
Returns the value of attribute token.
Class Method Summary collapse
-
.set_private_key(key) ⇒ Object
key
can be a File, String or OpenSSL::Pkey::RSA Sets the private key to use for secure sessions.
Instance Method Summary collapse
-
#delete(url) ⇒ Object
delete
url
Does a HTTP DELETE request to Google using the AuthSub token. -
#get(url) ⇒ Object
get urls Does a HTTP GET request to Google using the AuthSub token.
-
#initialize(opts = {}) ⇒ GoogleAuthSub
constructor
Create a new GoogleAuthsub object Options specified in
opts
consist of:. -
#post(url) ⇒ Object
post
url
Does a HTTP POST request to Google using the AuthSub token. -
#put(url) ⇒ Object
put
url
Does a HTTP PUT request to Google using the AuthSub token. -
#receive_token(url) ⇒ Object
url
:the URL received from Google once the user has signed in. -
#request_session_token ⇒ Object
request_session_token This method exchanges a previously received single use token with a session token.
-
#request_url ⇒ Object
This returns a URI::HTTPS object which contains the Google url to request a token from.
-
#revoke_token ⇒ Object
revoke_token This revokes either a single use or session token The token will not be able to be used again if this call is successful.
-
#secure_token? ⇒ Boolean
Returns true if the token is used for secure sessions.
-
#session_token? ⇒ Boolean
Returns true if this token can be exchanged for a session token.
-
#token_info ⇒ Object
token_info Returns the information for the session token from Google.
Constructor Details
#initialize(opts = {}) ⇒ GoogleAuthSub
Create a new GoogleAuthsub object Options specified in opts
consist of:
-
:next_url - (String) The url to redirect back to once the user has signed in to Google.
-
:scope_url - (String) The service from Google that you wish to receive data from with this token.
-
:session - (boolean) Whether the token is able to be used to get a session token or is just one use.
-
:secure - (boolean) Whether the token can be used for sessions.
-
:sigalg - (String) Currently not needed, as the Authsub specification only has rsa-sha1.
88 89 90 91 92 93 94 |
# File 'lib/googleauthsub.rb', line 88 def initialize(opts = {}) self.next_url = opts[:next_url] || '' self.scope = opts[:scope_url] || '' self.session = opts[:session] || false self.secure = opts[:secure] || false self.sigalg = opts[:sigalg] || "rsa-sha1" end |
Instance Attribute Details
#next_url ⇒ Object
Returns the value of attribute next_url.
54 55 56 |
# File 'lib/googleauthsub.rb', line 54 def next_url @next_url end |
#scope ⇒ Object
Returns the value of attribute scope.
54 55 56 |
# File 'lib/googleauthsub.rb', line 54 def scope @scope end |
#secure ⇒ Object
Returns the value of attribute secure.
54 55 56 |
# File 'lib/googleauthsub.rb', line 54 def secure @secure end |
#session ⇒ Object
Returns the value of attribute session.
54 55 56 |
# File 'lib/googleauthsub.rb', line 54 def session @session end |
#sigalg ⇒ Object
Returns the value of attribute sigalg.
54 55 56 |
# File 'lib/googleauthsub.rb', line 54 def sigalg @sigalg end |
#target ⇒ Object
Returns the value of attribute target.
54 55 56 |
# File 'lib/googleauthsub.rb', line 54 def target @target end |
#token ⇒ Object
Returns the value of attribute token.
54 55 56 |
# File 'lib/googleauthsub.rb', line 54 def token @token end |
Class Method Details
.set_private_key(key) ⇒ Object
key
can be a File, String or OpenSSL::Pkey::RSA Sets the private key to use for secure sessions. This should correspond to the public key sent to Google in the registration process. For registration details see:
http://code.google.com/apis/accounts/docs/RegistrationForWebAppsAuto.html
This sets the class variable @@pkey to an OpenSSL::Pkey::RSA object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/googleauthsub.rb', line 65 def self.set_private_key(key) case key when OpenSSL::PKey::RSA @@pkey = key when File # Read key from a PEM file. @@pkey = OpenSSL::PKey::RSA.new(key.read) when String # Get key from a PEM in the form of a string. @@pkey = OpenSSL::PKey::RSA.new(key) else raise AuthSubError, "Private Key in wrong format. Require IO, String or OpenSSL::Pkey::RSA, you gave me #{key.class}" end end |
Instance Method Details
#delete(url) ⇒ Object
delete url
Does a HTTP DELETE request to Google using the AuthSub token. This returns a Net::HTTPResponse object.
200 201 202 |
# File 'lib/googleauthsub.rb', line 200 def delete(url) authsub_http_request(Net::HTTP::Delete,url) end |
#get(url) ⇒ Object
get urls Does a HTTP GET request to Google using the AuthSub token. This returns a Net::HTTPResponse object.
179 180 181 |
# File 'lib/googleauthsub.rb', line 179 def get(url) authsub_http_request(Net::HTTP::Get,url) end |
#post(url) ⇒ Object
post url
Does a HTTP POST request to Google using the AuthSub token. This returns a Net::HTTPResponse object.
186 187 188 |
# File 'lib/googleauthsub.rb', line 186 def post(url) authsub_http_request(Net::HTTP::Post,url) end |
#put(url) ⇒ Object
put url
Does a HTTP PUT request to Google using the AuthSub token. This returns a Net::HTTPResponse object.
193 194 195 |
# File 'lib/googleauthsub.rb', line 193 def put(url) authsub_http_request(Net::HTTP::Put,url) end |
#receive_token(url) ⇒ Object
url
:the URL received from Google once the user has signed in.
This method extracts the token from the request url that Google sends the user back to. This url will be like: www.example.com/next?Token=CMDshfjfkeodf In Rails applications you don’t need this method, just use GoogleAuthsub#token=params
113 114 115 116 117 |
# File 'lib/googleauthsub.rb', line 113 def receive_token(url) raise AuthSubError, "receive_token() was not passed a url, #{@url.class} received instead." if !url.class == URI::HTTP q = url.query.match( /.*token=(.*)/i) if !url.query.nil? @token = q[1] if !q.nil? end |
#request_session_token ⇒ Object
request_session_token This method exchanges a previously received single use token with a session token. Raises error if an invalid response is received.
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/googleauthsub.rb', line 132 def request_session_token url = URI::HTTPS.build({:host => GOOGLE_HOST_URL, :path => GOOGLE_AUTHSUB_SESSION_TOKEN_PATH}) begin response = get(url) rescue raise AuthSubError, "Invalid session token response." end @token = response.body.match(/^Token=(.*)$/)[1] end |
#request_url ⇒ Object
This returns a URI::HTTPS object which contains the Google url to request a token from.
97 98 99 100 101 102 103 |
# File 'lib/googleauthsub.rb', line 97 def request_url raise AuthSubError, "Invalid next URL: #{@next_url}" if !full_url?(@next_url) query = "next=" << @next_url << "&scope=" << @scope << "&session="<< (session_token? ? '1' : '0')<< "&secure="<< (secure_token? ? '1' : '0') query = URI.encode(query) URI::HTTPS.build({:host => GOOGLE_HOST_URL, :path => GOOGLE_AUTHSUB_REQUEST_PATH, :query => query }) end |
#revoke_token ⇒ Object
revoke_token This revokes either a single use or session token The token will not be able to be used again if this call is successful. It returns true on sucess, false on failure.
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/googleauthsub.rb', line 147 def revoke_token url = URI::HTTPS.build({:host=>GOOGLE_HOST_URL, :path => GOOGLE_AUTHSUB_REVOKE_PATH}) begin response = get(url) true rescue false end end |
#secure_token? ⇒ Boolean
Returns true if the token is used for secure sessions
125 126 127 |
# File 'lib/googleauthsub.rb', line 125 def secure_token? secure == true end |
#session_token? ⇒ Boolean
Returns true if this token can be exchanged for a session token
120 121 122 |
# File 'lib/googleauthsub.rb', line 120 def session_token? session == true end |
#token_info ⇒ Object
token_info Returns the information for the session token from Google. Returns a map :scope, :secure
161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/googleauthsub.rb', line 161 def token_info url = URI::HTTPS.build({:host=>GOOGLE_HOST_URL, :path => GOOGLE_AUTHSUB_TOKEN_INFO_PATH}) response = get(url) info = Hash.new begin info[:target] = response.body.match(/^Target=(.*)$/)[1] info[:scope] = response.body.match(/^Scope=(.*)$/)[1] info[:secure] = (response.body.match(/^Secure=(.*)$/)[1].downcase == 'true') rescue raise AuthSubError, "Google Authsub Error: invalid token info packet received." end return info end |