Class: RubynCode::Auth::OAuth

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

Defined Under Namespace

Classes: RefreshError, StateMismatchError, TokenExchangeError

Constant Summary collapse

VERIFIER_LENGTH =
43

Instance Method Summary collapse

Instance Method Details

#authenticate!Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubyn_code/auth/oauth.rb', line 23

def authenticate!
  code_verifier = generate_code_verifier
  code_challenge = derive_code_challenge(code_verifier)
  state = SecureRandom.hex(24)

  result = perform_browser_auth(code_challenge, state)
  validate_state!(result[:state], state)

  tokens = exchange_code(code: result[:code], code_verifier:)
  persist_tokens(tokens)
  tokens
end

#refresh!Object

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubyn_code/auth/oauth.rb', line 36

def refresh!
  stored = TokenStore.load
  raise RefreshError, 'No stored refresh token available' unless stored&.dig(:refresh_token)

  response = post_refresh_request(stored[:refresh_token])
  raise_refresh_error(response) unless response.success?

  body = parse_json(response.body)
  raise RefreshError, 'Invalid response from token endpoint' unless body

  save_refreshed_tokens(body, stored)
end