Skip to content
This repository was archived by the owner on Jan 27, 2023. It is now read-only.

Commit 83bb5d0

Browse files
authored
Merge pull request #23 from cipherstash/add-log-in-method
Add `CipherStash::Client.login` class method
2 parents 980341e + 9b288eb commit 83bb5d0

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

lib/cipherstash/client.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@ def self.client_options
3131
Profile.profile_options
3232
end
3333

34+
35+
# Log into a CipherStash workspace.
36+
#
37+
# If `workspace` is given, a profile will be created for that workspace.
38+
# This is useful for logging into a workspace for the first time.
39+
#
40+
# If a workspace is not provided, this command will load an existing profile and then log in using that profile.
41+
# The profile name defaults to "default", but can be overridden with the `CS_PROFILE_NAME` env var or the `defaultProfile` opt in `{cs_config_path}/config.json`.
42+
#
43+
# @return [TrueClass]
44+
#
45+
# @raise [CipherStash::Client::Error::CreateProfileFailure] if a workspace is given and a new profile could not be created.
46+
#
47+
# @raise [CipherStash::Client::Error::LoadProfileFailure] if a workspace is not given and an existing profile could not be loaded.
48+
#
49+
def self.login(workspace: nil, profile_name: nil, logger: default_logger)
50+
Profile.login(workspace: workspace, profile_name: profile_name, logger: logger)
51+
true
52+
end
53+
3454
# Create a new CipherStash client.
3555
#
3656
# No options are necessary in the common case.
@@ -79,7 +99,7 @@ def self.client_options
7999
#
80100
def initialize(profileName: Unspecified, logger: Unspecified, metrics: Metrics::Null.new, rpc_class: RPC, **opts)
81101
@logger = if logger == Unspecified
82-
Logger.new($stderr).tap { |l| l.level = Logger::WARN; l.formatter = ->(_, _, _, m) { "#{m}\n" } }
102+
self.class.default_logger
83103
else
84104
logger
85105
end
@@ -249,6 +269,13 @@ def generate_naming_key
249269

250270
private
251271

272+
def self.default_logger
273+
Logger.new($stderr).tap do |l|
274+
l.level = Logger::WARN
275+
l.formatter = ->(_, _, _, m) { "#{m}\n" }
276+
end
277+
end
278+
252279
def console
253280
@profile.with_access_token do |token|
254281
Console.new(access_token: token[:access_token], logger: @logger)

lib/cipherstash/client/profile.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ def self.create(name, logger, **opts)
9292
if name == "default"
9393
incoming_workspace_id = opts[:workspace]
9494

95-
9695
profile_config = File.read(File.expand_path("~/.cipherstash/#{name}/profile-config.json"))
9796
parsed_profile_config = JSON.parse(profile_config)
9897

@@ -127,6 +126,17 @@ def self.create(name, logger, **opts)
127126
profile.save
128127
end
129128

129+
def self.login(workspace:, profile_name:, logger:)
130+
is_initial_login = !workspace.nil?
131+
profile_name = resolve_profile_name(profile_name)
132+
133+
if is_initial_login
134+
create(profile_name, logger, workspace: workspace)
135+
else
136+
load(profile_name, logger).login
137+
end
138+
end
139+
130140
class << self
131141
private
132142

@@ -530,6 +540,13 @@ def generate_naming_key
530540
end
531541
end
532542

543+
def login
544+
access_token_creds_provider = access_token_provider(**symbolize_keys(identity_provider_config))
545+
access_token_creds_provider.fresh_credentials
546+
547+
self
548+
end
549+
533550
private
534551

535552
def access_token_provider(kind:, **opts)

0 commit comments

Comments
 (0)