SdkSession

Class: SdkSession

Version 1.6 - Updated on 03/23/2017

Contents

Overview

SdkSession is responsible for storing OAuth 1.0 information. It knows how to take your consumer key and secret and request an access token, or an authorize url, and request an access token. You just need to pass it to SdkClient after its been authorized.

Instance Attribute Summary

Attributes inherited from SdkSessionBase:

  • #locale

Class Method Summary

  • .deserialize(ser) ⇒ Object

Takes a serialized SdkSession YAML String and returns a new SdkSession object.

Constructor Details

#initialize(consumer_key, consumer_secret, locale = nil) ⇒ SdkSession

  • consumer_key - Your application’s Key.
  • consumer_secret - Your application’s Secret.
 def initialize(consumer_key, consumer_secret, locale=nil)
      super(locale)
      @consumer_key = consumer_key
      @consumer_secret = consumer_secret
      @request_token = nil
      @access_token = nil
    end
    

Class Method Details

.deserialize(ser) ⇒ Object

Takes a serialized SdkSession YAML String and returns a new SdkSession object.

 def self.deserialize(ser)
      ser = YAML::load(ser)
      session = SdkSession.new(ser.pop, ser.pop)
      session.set_request_token(ser.pop, ser.pop)
    
      if ser.length > 0
        session.set_access_token(ser.pop, ser.pop)
      end
      session
    end
    

Instance Method Details

access_token

#access_token ⇒ Object

Returns the access token, or nil if one hasn’t been acquired yet.

 def access_token
      @access_token
    end
    

assert_authorized

#assert_authorized ⇒ Object

If there is a valid an access token, then do nothing. If not, throw a RuntimeError.

 def assert_authorized
      unless authorized?
        raise RuntimeError.new('Session does not yet have a request token')
      end
    end
    

authorized?

#authorized? ⇒ Boolean

Returns true if this Session has been authorized and has avalid access_token.

  • Returns:
    • (Boolean)
 def authorized?
      !!@access_token
    end
    

clear_access_token

#clear_access_token ⇒ Object

Clears the access_token.

 def clear_access_token
      @access_token = nil
    end
    

get_access_token

#get_access_token ⇒ Object

Returns the access token. If this SdkSession doesn’t yet have an access_token, one is requested using the request_token generate from your app’s token and secret. This request fails unless your user has granted your app access to his or her Personal Cloud Storage content using authorize_url.

 def get_access_token
      return @access_token if authorized?
    
      if @request_token.nil?
        raise RuntimeError.new("No request token. You must set this or get an authorize url first.")
      end
    
      @access_token = get_token("/access_token", @request_token,  "Couldn't get access token.")
    end
    

get_authorize_url

#get_authorize_url(callback = nil) ⇒ Object

This method returns a URL that the user must visit to grant your app access to his or her Personal Cloud Storage content.

 def get_authorize_url(callback=nil)
      get_request_token()
        logger = Logger.new(STDOUT)
        
      url = "/#{Sdk::API_VERSION}/oauth/authorize?oauth_token=#{URI.escape(@request_token.key)}"
      logger.info url
      if callback
        url += "&oauth_callback=#{URI.escape(callback)}"
      end
      if @locale
        url += "&locale=#{URI.escape(@locale)}"
      end
      
      "https://#{Sdk::WEB_SERVER}#{url}"
    end
    

get_request_token

#get_request_token ⇒ Object

This method returns a request token. Requests one from the server using the provided application key and secret if nessecary.

 def get_request_token()
      @request_token ||= get_token("/request_token", nil, "Error getting request token.  Is your app key and secret correctly set?")
    end
    

get_token

#get_token(url_end, input_token, error_message_prefix) ⇒ Object

 def get_token(url_end, input_token, error_message_prefix) #: nodoc:
      response = do_get_with_token("https://#{Sdk::API_SERVER}/#{Sdk::API_VERSION}/oauth#{url_end}", input_token)
      if not response.kind_of?(Net::HTTPSuccess) # it must be a 200
        raise SdkAuthError.new("#{error_message_prefix}  Server returned #{response.code}: #{response.message}.", response)
      end
      parts = CGI.parse(response.body)
    
      if !parts.has_key? "oauth_token" and parts["oauth_token"].length != 1
        raise SdkAuthError.new("Invalid response from #{url_end}: missing \"oauth_token\" parameter: #{response.body}", response)
      end
      if !parts.has_key? "oauth_token_secret" and parts["oauth_token_secret"].length != 1
        raise SdkAuthError.new("Invalid response from #{url_end}: missing \"oauth_token\" parameter: #{response.body}", response)
      end
    
      OAuthToken.new(parts["oauth_token"][0], parts["oauth_token_secret"][0])
    end
    

request_token

#request_token ⇒ Object

Returns the request token, or nil if one hasn’t been acquired yet.

 def request_token
      @request_token
    end
    

serialize

#serialize ⇒ Object

Serializes the SdkSession. At SdkSession’s state is capture in three key and secret pairs. Consumer, request, and access. Serialize returns these items in a YAML string, generated from a converted array in this form:

 consumer_key, consumer_secret, request_token.token, request_token.secret, access_token.token, access_token.secret
    

NOTE: access_token is only included if it already exists in the SdkSesssion.

 def serialize
      toreturn = []
      if @access_token
        toreturn.push @access_token.secret, @access_token.key
      end
    
      get_request_token
    
      toreturn.push @request_token.secret, @request_token.key
      toreturn.push @consumer_secret, @consumer_key
    
      toreturn.to_yaml
    end
    

set_access_token

#set_access_token(key, secret) ⇒ Object

Given a saved access token and secret, set this Session to use that token and secret.

  • token - Access token
  • secret - Access token secret
 def set_access_token(key, secret)
      @access_token = OAuthToken.new(key, secret)
    end
    

set_request_token

#set_request_token(key, secret) ⇒ Object

Given a saved request token and secret, set this location’s token and secret.

  • token - Request token
  • secret - Request token secret
 def set_request_token(key, secret)
      @request_token = OAuthToken.new(key, secret)
    end
    

Copyright © 2015-2017, Verizon and/or its Licensors. All rights reserved.