Module: Webhookdb::Replicator::OAuthRefreshAccessTokenMixin

Defined in:
lib/webhookdb/replicator/oauth_refresh_access_token_mixin.rb

Constant Summary collapse

EXPIRATION_BUFFER =
60

Instance Method Summary collapse

Instance Method Details

#_with_oauth_access_token(oauth_user_id, get_refresh_token) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/webhookdb/replicator/oauth_refresh_access_token_mixin.rb', line 25

def _with_oauth_access_token(oauth_user_id, get_refresh_token)
  key = self.oauth_access_token_cache_key(oauth_user_id)
  Webhookdb::Redis.cache.with do |r|
    got = r.call("GET", key)
    if got
      yield got
    else
      self.logger.debug "creating_access_token", access_token_cache_key: key
      form_body = URI.encode_www_form(
        {
          client_id: self.service_integration.backfill_key,
          client_secret: self.service_integration.backfill_secret,
          refresh_token: get_refresh_token.call,
          grant_type: "refresh_token",
        },
      )
      resp = Webhookdb::Http.post(
        self.oauth_token_url,
        form_body,
        headers: {
          "Content-Type" => "application/x-www-form-urlencoded",
          "charset" => "utf-8",
        },
        logger: self.logger,
        timeout: self.oauth_http_timeout,
      )
      access_token = resp.parsed_response.fetch("access_token")
      r.call("SETEX", key, resp.parsed_response["expires_in"] - EXPIRATION_BUFFER, access_token)
      yield access_token
    end
  end
end

#delete_oauth_access_token(oauth_user_id) ⇒ Object



18
19
20
21
22
23
# File 'lib/webhookdb/replicator/oauth_refresh_access_token_mixin.rb', line 18

def delete_oauth_access_token(oauth_user_id)
  key = self.oauth_access_token_cache_key(oauth_user_id)
  Webhookdb::Redis.cache.with do |r|
    r.call("DEL", key)
  end
end

#force_set_oauth_access_token(oauth_user_id, access_token, expires_in: 60.minutes.to_i) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/webhookdb/replicator/oauth_refresh_access_token_mixin.rb', line 58

def force_set_oauth_access_token(oauth_user_id, access_token, expires_in: 60.minutes.to_i)
  key = self.oauth_access_token_cache_key(oauth_user_id)
  Webhookdb::Redis.cache.with do |r|
    r.call("SETEX", key, expires_in, access_token)
    return access_token
  end
end

#oauth_access_token_cache_key(oauth_user_id) ⇒ Object



13
14
15
16
# File 'lib/webhookdb/replicator/oauth_refresh_access_token_mixin.rb', line 13

def oauth_access_token_cache_key(oauth_user_id)
  parts = [self.oauth_cache_key_namespace, "atok", self.service_integration.id.to_s, oauth_user_id]
  return Webhookdb::Redis.cache_key(parts)
end

#oauth_cache_key_namespaceObject



8
# File 'lib/webhookdb/replicator/oauth_refresh_access_token_mixin.rb', line 8

def oauth_cache_key_namespace = raise NotImplementedError("return something like 'gcalv1'")

#oauth_http_timeoutObject



11
# File 'lib/webhookdb/replicator/oauth_refresh_access_token_mixin.rb', line 11

def oauth_http_timeout = raise NotImplementedError("return a timeout value for oauth http requests")

#oauth_token_urlObject



9
# File 'lib/webhookdb/replicator/oauth_refresh_access_token_mixin.rb', line 9

def oauth_token_url = raise NotImplementedError("return something like https://someservice/oauth2/token")