Class: Onetime::Resources::Secrets

Inherits:
Object
  • Object
show all
Defined in:
lib/onetime/resources/secrets.rb

Overview

Secret operations: conceal, generate, reveal, show, status.

The request/response contracts differ between API versions:

- v1 uses flat form-encoded params and legacy endpoint names
(/share, /generate, POST /secret/:key for reveal).
- v2 nests create params under a `secret` object, uses JSON bodies,
and exposes richer REST endpoints (/secret/conceal, /secret/:id, ...).

The version-specific branches are kept side by side in each method so the differences are easy to audit.

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Secrets

Returns a new instance of Secrets.



16
17
18
# File 'lib/onetime/resources/secrets.rb', line 16

def initialize(client)
  @client = client
end

Instance Method Details

#conceal(secret:, ttl: nil, passphrase: nil, recipient: nil, share_domain: nil, guest: false) ⇒ Object Also known as: share

Conceal (share) a secret value you already have.

Parameters:

  • secret (String)

    the secret content

  • ttl (Integer, nil) (defaults to: nil)

    time-to-live in seconds

  • passphrase (String, nil) (defaults to: nil)

    passphrase required to reveal

  • recipient (String, Array<String>, nil) (defaults to: nil)

    email recipient(s)

  • share_domain (String, nil) (defaults to: nil)

    custom share domain

  • guest (Boolean) (defaults to: false)

    use the anonymous /guest/* endpoint (v2)



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/onetime/resources/secrets.rb', line 28

def conceal(secret:, ttl: nil, passphrase: nil, recipient: nil, share_domain: nil, guest: false)
  case version
  when :v1
    form = compact(secret: secret, ttl: ttl, passphrase: passphrase,
                   recipient: recipient, share_domain: share_domain)
    @client.request(:post, "/share", form: form)
  when :v2
    path = guest ? "/guest/secret/conceal" : "/secret/conceal"
    @client.request(:post, path, body: { secret: secret_payload(
      secret: secret, ttl: ttl, passphrase: passphrase,
      recipient: recipient, share_domain: share_domain
    ) })
  end
end

#generate(ttl: nil, passphrase: nil, recipient: nil, share_domain: nil, guest: false) ⇒ Object

Generate a random secret value server-side.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/onetime/resources/secrets.rb', line 45

def generate(ttl: nil, passphrase: nil, recipient: nil, share_domain: nil, guest: false)
  case version
  when :v1
    form = compact(ttl: ttl, passphrase: passphrase,
                   recipient: recipient, share_domain: share_domain)
    @client.request(:post, "/generate", form: form)
  when :v2
    path = guest ? "/guest/secret/generate" : "/secret/generate"
    @client.request(:post, path, body: { secret: secret_payload(
      ttl: ttl, passphrase: passphrase,
      recipient: recipient, share_domain: share_domain
    ) })
  end
end

#reveal(key, passphrase: nil, continue: true, guest: false) ⇒ Object

Reveal (consume) a secret by its key. This is a one-time action.

Parameters:

  • key (String)

    the secret identifier

  • passphrase (String, nil) (defaults to: nil)

    passphrase if one was set

  • continue (Boolean) (defaults to: true)

    confirm the destructive reveal



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/onetime/resources/secrets.rb', line 65

def reveal(key, passphrase: nil, continue: true, guest: false)
  identifier = extract_secret_key(key)
  case version
  when :v1
    form = compact(passphrase: passphrase, continue: continue)
    @client.request(:post, "/secret/#{identifier}", form: form)
  when :v2
    path = guest ? "/guest/secret/#{identifier}/reveal" : "/secret/#{identifier}/reveal"
    @client.request(:post, path, body: compact(passphrase: passphrase, continue: continue))
  end
end

#show(key, guest: false) ⇒ Object

Show a secret's metadata without revealing its value. (v2 only)



78
79
80
81
82
83
# File 'lib/onetime/resources/secrets.rb', line 78

def show(key, guest: false)
  require_version!(:v2, "secrets.show")
  identifier = extract_secret_key(key)
  path = guest ? "/guest/secret/#{identifier}" : "/secret/#{identifier}"
  @client.request(:get, path)
end

#status(key) ⇒ Object

Check a single secret's status. (v2 only)



86
87
88
89
# File 'lib/onetime/resources/secrets.rb', line 86

def status(key)
  require_version!(:v2, "secrets.status")
  @client.request(:get, "/secret/#{extract_secret_key(key)}/status")
end

#status_list(keys) ⇒ Object

Check the status of multiple secrets in one call. (v2 only)

Parameters:

  • keys (Array<String>, String)

    identifiers (array or CSV string)



94
95
96
97
98
# File 'lib/onetime/resources/secrets.rb', line 94

def status_list(keys)
  require_version!(:v2, "secrets.status_list")
  identifiers = keys.is_a?(Array) ? keys.join(",") : keys.to_s
  @client.request(:post, "/secret/status", body: { identifiers: identifiers })
end