Class: ZohoHub::Auth

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/zoho_hub/auth.rb

Overview

Class that takes care of authentication using Oauth2 workflow as described here: www.zoho.com/crm/help/api/v2/#oauth-request.

Constant Summary collapse

TOKEN_PATH =
'/oauth/v2/token'
REVOKE_TOKEN_PATH =
'/oauth/v2/token/revoke'
AUTH_PATH =
'/oauth/v2/auth'
DEFAULT_SCOPES =
%w[
  ZohoCRM.modules.custom.all
  ZohoCRM.settings.all
  ZohoCRM.modules.contacts.all
  ZohoCRM.modules.all
].freeze
DEFAULT_ACCESS_TYPE =
'offline'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_type: DEFAULT_ACCESS_TYPE, scopes: DEFAULT_SCOPES) ⇒ Auth

Returns a new instance of Auth.



29
30
31
32
33
# File 'lib/zoho_hub/auth.rb', line 29

def initialize(access_type: DEFAULT_ACCESS_TYPE, scopes: DEFAULT_SCOPES)
  @configuration = ZohoHub.configuration
  @access_type = access_type
  @scopes = scopes
end

Class Method Details

.auth_url(access_type: DEFAULT_ACCESS_TYPE, scopes: DEFAULT_SCOPES) ⇒ Object



35
36
37
# File 'lib/zoho_hub/auth.rb', line 35

def self.auth_url(access_type: DEFAULT_ACCESS_TYPE, scopes: DEFAULT_SCOPES)
  new(access_type: access_type, scopes: scopes).auth_url
end

.get_token(grant_token) ⇒ Object



102
103
104
# File 'lib/zoho_hub/auth.rb', line 102

def self.get_token(grant_token)
  new.get_token(grant_token)
end

.refresh_token(refresh_token) ⇒ Object



62
63
64
# File 'lib/zoho_hub/auth.rb', line 62

def self.refresh_token(refresh_token)
  new.refresh_token(refresh_token)
end

Instance Method Details

#auth_full_uriObject



58
59
60
# File 'lib/zoho_hub/auth.rb', line 58

def auth_full_uri
  Addressable::URI.join(api_domain, AUTH_PATH)
end

#auth_urlObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/zoho_hub/auth.rb', line 39

def auth_url
  uri = auth_full_uri

  query = {
    client_id: client_id,
    scope: @scopes.join(','),
    access_type: @access_type,
    redirect_uri: redirect_uri,
    response_type: 'code'
  }

  # The consent page must be presented otherwise we don't get the refresh token back.
  query[:prompt] = 'consent' if @access_type == DEFAULT_ACCESS_TYPE

  uri.query_values = query

  Addressable::URI.unencode(uri.to_s)
end

#get_token(grant_token) ⇒ Object



106
107
108
109
110
# File 'lib/zoho_hub/auth.rb', line 106

def get_token(grant_token)
  result = Faraday.post(token_url(grant_token))

  parse(result.body)
end

#refresh_token(refresh_token) ⇒ Object



66
67
68
69
70
71
# File 'lib/zoho_hub/auth.rb', line 66

def refresh_token(refresh_token)
  result = Faraday.post(refresh_url(refresh_token))

  json = parse(result.body)
  json.merge(refresh_token: refresh_token)
end

#refresh_url(refresh_token) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/zoho_hub/auth.rb', line 73

def refresh_url(refresh_token)
  uri = token_full_uri

  uri.query_values = {
    client_id: client_id,
    client_secret: secret,
    refresh_token: refresh_token,
    grant_type: 'refresh_token'
  }

  Addressable::URI.unencode(uri.to_s)
end

#revoke_refresh_token(refresh_token) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/zoho_hub/auth.rb', line 90

def revoke_refresh_token(refresh_token)
  uri = token_full_uri

  uri.query_values = { token: refresh_token }

  url = Addressable::URI.unencode(uri.to_s)

  result = Faraday.post(url)

  parse(result.body)
end

#token_full_uriObject



86
87
88
# File 'lib/zoho_hub/auth.rb', line 86

def token_full_uri
  Addressable::URI.join(api_domain, TOKEN_PATH)
end

#token_url(grant_token) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/zoho_hub/auth.rb', line 112

def token_url(grant_token)
  uri = token_full_uri

  uri.query_values = {
    client_id: client_id,
    client_secret: secret,
    code: grant_token,
    redirect_uri: redirect_uri,
    grant_type: 'authorization_code'
  }

  Addressable::URI.unencode(uri.to_s)
end