Sha256: 6ef2381e0bd94f27c276bca7fe031637e9619b8048d30a915590ecfd371921c7

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module Typetalk
  class Api

    module Auth
      attr_accessor :authorization_code

      def get_access_token(client_id:nil, client_secret:nil, grant_type:nil, scope:nil, code:nil, redirect_uri:nil)
        body = {
          client_id: client_id || Typetalk.config.client_id,
          client_secret: client_secret || Typetalk.config.client_secret,
          grant_type: grant_type || Typetalk.config.grant_type,
          scope: scope || Typetalk.config.scope,
        }

        if body[:grant_type] == 'authorization_code'
          body[:code] = code || @authorization_code
          body[:redirect_uri] = redirect_uri || Typetalk.config.redirect_uri
        end

        response = connection.post do |req|
         req.url 'https://typetalk.in/oauth2/access_token'
         req.body = body
        end
        parse_response(response)
      end


      def self.authorize_url(client_id:nil, redirect_uri:nil, scope:nil)
        params = {
          client_id: client_id || Typetalk.config.client_id,
          redirect_uri: redirect_uri || Typetalk.config.redirect_uri,
          scope: scope || Typetalk.config.scope,
          response_type: 'code',
        }
        url = URI.parse('https://typetalk.in/oauth2/authorize')
        url.query = URI.encode_www_form(params)
        url.to_s
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
typetalk-0.0.1 lib/typetalk/api/auth.rb