Class: SmilyCli::OAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/smily_cli/oauth.rb

Overview

OAuth 2.0 helper for obtaining and refreshing access tokens against the BookingSync token endpoint (<base_url>/oauth/token). Supports the flows documented in the API reference: client credentials, refresh token, and the authorization-code exchange (plus building the authorize URL for the interactive step of that flow).

Kept deliberately on Net::HTTP so token acquisition has no dependency on the API client's connection stack.

Defined Under Namespace

Classes: Token

Instance Method Summary collapse

Constructor Details

#initialize(base_url: Context::DEFAULT_BASE_URL, verbose: false) ⇒ OAuth

Returns a new instance of OAuth.

Parameters:

  • base_url (String) (defaults to: Context::DEFAULT_BASE_URL)

    site base URL

  • verbose (Boolean) (defaults to: false)


32
33
34
35
# File 'lib/smily_cli/oauth.rb', line 32

def initialize(base_url: Context::DEFAULT_BASE_URL, verbose: false)
  @base_url = base_url
  @verbose = verbose
end

Instance Method Details

#authorization_code(client_id:, client_secret:, code:, redirect_uri:) ⇒ Token

Exchange an authorization code (from the redirect) for tokens.

Returns:



66
67
68
69
70
71
72
73
74
# File 'lib/smily_cli/oauth.rb', line 66

def authorization_code(client_id:, client_secret:, code:, redirect_uri:)
  request_token(
    grant_type: "authorization_code",
    client_id: client_id,
    client_secret: client_secret,
    code: code,
    redirect_uri: redirect_uri
  )
end

#authorize_url(client_id:, redirect_uri:, scope: nil, state: nil, account_id: nil, response_type: "code") ⇒ String

Build the URL a user visits to authorize the app (step 1 of the authorization-code / implicit flows).

Parameters:

  • response_type (String) (defaults to: "code")

    "code" (server-side) or "token" (implicit)

Returns:

  • (String)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/smily_cli/oauth.rb', line 81

def authorize_url(client_id:, redirect_uri:, scope: nil, state: nil, account_id: nil, response_type: "code")
  params = {
    client_id: client_id,
    redirect_uri: redirect_uri,
    response_type: response_type,
    scope: scope,
    state: state,
    account_id: 
  }.reject { |_, v| v.nil? || v.to_s.empty? }

  uri = URI.join(@base_url, "/oauth/authorize")
  uri.query = URI.encode_www_form(params)
  uri.to_s
end

#client_credentials(client_id:, client_secret:, scope: nil) ⇒ Token

Client Credentials flow — an app-level token limited to the public scope, able to read across every account that authorized the app.

Returns:



41
42
43
44
45
46
47
48
# File 'lib/smily_cli/oauth.rb', line 41

def client_credentials(client_id:, client_secret:, scope: nil)
  request_token(
    grant_type: "client_credentials",
    client_id: client_id,
    client_secret: client_secret,
    scope: scope
  )
end

#refresh(client_id:, client_secret:, refresh_token:, redirect_uri: nil) ⇒ Token

Refresh an access token using a refresh token.

Returns:



53
54
55
56
57
58
59
60
61
# File 'lib/smily_cli/oauth.rb', line 53

def refresh(client_id:, client_secret:, refresh_token:, redirect_uri: nil)
  request_token(
    grant_type: "refresh_token",
    client_id: client_id,
    client_secret: client_secret,
    refresh_token: refresh_token,
    redirect_uri: redirect_uri
  )
end