Module: RubyCoded::Auth::JWTDecoder

Defined in:
lib/ruby_coded/auth/jwt_decoder.rb

Overview

Decodes JWT tokens without external dependencies. Used to extract the ChatGPT account ID from OAuth access tokens.

Constant Summary collapse

JWT_CLAIM_PATH =
"https://api.openai.com/auth"

Class Method Summary collapse

Class Method Details

.decode(token) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/ruby_coded/auth/jwt_decoder.rb', line 13

def self.decode(token)
  parts = token.to_s.split(".")
  return nil unless parts.size == 3

  padded = parts[1] + ("=" * ((4 - (parts[1].length % 4)) % 4))
  JSON.parse(Base64.urlsafe_decode64(padded))
rescue StandardError
  nil
end

.extract_account_id(token) ⇒ Object



23
24
25
26
# File 'lib/ruby_coded/auth/jwt_decoder.rb', line 23

def self.(token)
  payload = decode(token)
  payload&.dig(JWT_CLAIM_PATH, "chatgpt_account_id")
end