Class: ZohoHub::Auth

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

Constant Summary collapse

TOKEN_PATH =
'/oauth/v2/token'
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: nil, scopes: nil) ⇒ Auth



26
27
28
29
30
# File 'lib/zoho_hub/auth.rb', line 26

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

Class Method Details

.get_token(grant_token) ⇒ Object



104
105
106
# File 'lib/zoho_hub/auth.rb', line 104

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

.refresh_token(refresh_token) ⇒ Object



81
82
83
# File 'lib/zoho_hub/auth.rb', line 81

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

Instance Method Details

#auth_full_uriObject



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

def auth_full_uri
  Addressable::URI.join(@configuration.base_url, AUTH_PATH)
end

#auth_urlObject



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

def auth_url
  uri = auth_full_uri

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

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

#get_token(grant_token) ⇒ Object



108
109
110
111
112
# File 'lib/zoho_hub/auth.rb', line 108

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

  parse(result.body)
end

#parse(body) ⇒ Object



114
115
116
# File 'lib/zoho_hub/auth.rb', line 114

def parse(body)
  MultiJson.load(body, symbolize_keys: true)
end

#refresh_token(refresh_token) ⇒ Object



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

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



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/zoho_hub/auth.rb', line 68

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



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

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



32
33
34
# File 'lib/zoho_hub/auth.rb', line 32

def token_full_uri
  Addressable::URI.join(@configuration.base_url, TOKEN_PATH)
end

#token_url(grant_token) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/zoho_hub/auth.rb', line 54

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