Class: Foobara::Auth::CreateToken

Inherits:
Command
  • Object
show all
Defined in:
foobara-auth-0.0.9/src/create_token.rb

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary collapse

Attributes included from CommandPatternImplementation::Concerns::Subcommands

#is_subcommand

Attributes included from CommandPatternImplementation::Concerns::Runtime

#exception, #outcome

Attributes included from CommandPatternImplementation::Concerns::Errors

#error_collection

Attributes included from CommandPatternImplementation::Concerns::Inputs

#inputs, #raw_inputs

Instance Method Summary collapse

Methods inherited from Command

install!, reset_all

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Methods included from CommandPatternImplementation::Concerns::Reflection

#initialize

Methods included from CommandPatternImplementation::Concerns::DomainMappers

#domain_map, #domain_map!, #run_mapped_subcommand!

Methods included from CommandPatternImplementation::Concerns::Subcommands

#run_subcommand!, #subcommand?

Methods included from CommandPatternImplementation::Concerns::Entities

#load_entities, #load_records

Methods included from CommandPatternImplementation::Concerns::Transactions

#auto_detect_current_transactions, #commit_transaction, #open_transaction, #opened_transactions, #relevant_entity_classes, #rollback_transaction, #transactions

Methods included from CommandPatternImplementation::Concerns::StateMachine

#state_machine

Methods included from CommandPatternImplementation::Concerns::Runtime

#halt!, #run, #run!, #run_execute, #succeed, #success?, #validate, #validate_records

Methods included from CommandPatternImplementation::Concerns::Errors

#initialize

Methods included from CommandPatternImplementation::Concerns::Inputs

#cast_and_validate_inputs, #initialize, #method_missing, #respond_to_missing?, #respond_to_missing_for_inputs?

Methods included from TruncatedInspect

#inspect, truncating

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Foobara::CommandPatternImplementation::Concerns::Inputs

Instance Attribute Details

#build_password_paramsObject

Returns the value of attribute build_password_params.



37
38
39
# File 'foobara-auth-0.0.9/src/create_token.rb', line 37

def build_password_params
  @build_password_params
end

#hashed_secretObject

Returns the value of attribute hashed_secret.



37
38
39
# File 'foobara-auth-0.0.9/src/create_token.rb', line 37

def hashed_secret
  @hashed_secret
end

#token_idObject

Returns the value of attribute token_id.



37
38
39
# File 'foobara-auth-0.0.9/src/create_token.rb', line 37

def token_id
  @token_id
end

#token_recordObject

Returns the value of attribute token_record.



37
38
39
# File 'foobara-auth-0.0.9/src/create_token.rb', line 37

def token_record
  @token_record
end

#token_secretObject

Returns the value of attribute token_secret.



37
38
39
# File 'foobara-auth-0.0.9/src/create_token.rb', line 37

def token_secret
  @token_secret
end

#token_stringObject

Returns the value of attribute token_string.



37
38
39
# File 'foobara-auth-0.0.9/src/create_token.rb', line 37

def token_string
  @token_string
end

Instance Method Details

#activate_tokenObject



87
88
89
# File 'foobara-auth-0.0.9/src/create_token.rb', line 87

def activate_token
  token_record.approve!
end

#construct_token_stringObject



91
92
93
# File 'foobara-auth-0.0.9/src/create_token.rb', line 91

def construct_token_string
  self.token_string = "#{token_id}_#{token_secret}"
end

#create_token_recordObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'foobara-auth-0.0.9/src/create_token.rb', line 64

def create_token_record
  attributes = {
    hashed_secret:,
    id: token_id,
    token_parameters: build_password_params.merge(other_params),
    created_at: Time.now
  }

  if expires_at
    attributes[:expires_at] = expires_at
  end

  if token_group
    attributes[:token_group] = token_group
  end

  self.token_record = Types::Token.create(attributes)
end

#executeObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'foobara-auth-0.0.9/src/create_token.rb', line 23

def execute
  generate_token_secret
  generate_hashed_secret
  generate_token_id
  construct_token_string
  create_token_record

  unless needs_approval?
    activate_token
  end

  token_string_and_record
end

#generate_hashed_secretObject



58
59
60
61
62
# File 'foobara-auth-0.0.9/src/create_token.rb', line 58

def generate_hashed_secret
  secret = run_subcommand!(BuildSecret, secret: token_secret)
  self.hashed_secret = secret.hashed_secret
  self.build_password_params = secret.parameters
end

#generate_token_idObject



39
40
41
42
43
44
45
46
47
# File 'foobara-auth-0.0.9/src/create_token.rb', line 39

def generate_token_id
  bytes = SecureRandom.random_bytes(token_id_bytes)

  begin
    token_id = Base64.strict_encode64(bytes)
  end while Types::Token.exists?(token_id)

  self.token_id = token_id
end

#generate_token_secretObject



49
50
51
52
# File 'foobara-auth-0.0.9/src/create_token.rb', line 49

def generate_token_secret
  bytes = SecureRandom.random_bytes(secret_bytes)
  self.token_secret = Base64.strict_encode64(bytes)
end

#needs_approval?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'foobara-auth-0.0.9/src/create_token.rb', line 83

def needs_approval?
  needs_approval
end

#other_paramsObject



102
103
104
# File 'foobara-auth-0.0.9/src/create_token.rb', line 102

def other_params
  { secret_bytes:, token_id_bytes: }
end

#secret_bytesObject



54
55
56
# File 'foobara-auth-0.0.9/src/create_token.rb', line 54

def secret_bytes
  32
end

#token_id_bytesObject



106
107
108
# File 'foobara-auth-0.0.9/src/create_token.rb', line 106

def token_id_bytes
  6
end

#token_string_and_recordObject



95
96
97
98
99
100
# File 'foobara-auth-0.0.9/src/create_token.rb', line 95

def token_string_and_record
  {
    token_string:,
    token_record:
  }
end