Sha256: 961a18ef1a76008849c37c9468970dae7215f2099d5d6247972a8e5c79ea98ab

Contents?: true

Size: 1.26 KB

Versions: 7

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require 'rbnacl'

module PUNK
  class ChallengeClaimService < Service
    args :session

    def validate
      validates_not_null :session
      validates_not_empty :session
      return if session.blank?
      validates_type Session, :session
      validates_state :session, :created
      validates_event :session, :challenge
    end

    def process
      secret = SecretService.run.result
      salt = RbNaCl::Random.random_bytes(RbNaCl::PasswordHash::SCrypt::SALTBYTES)
      hash = RbNaCl::PasswordHash.scrypt(secret, salt, 1_048_576, 16_777_216)
      session.update(salt: salt, hash: hash)
      session.challenge!
      identity = session.identity
      case identity.claim_type
      when :email
        SendEmailWorker.perform_async(
          from: 'GroupFire Accounts <noreply@groupfire.com>',
          to: identity.claim,
          subject: '[GroupFire] Verification Code',
          template: 'verify',
          tags: [:auth],
          variables: {
            name: identity.user&.name || 'New User',
            secret: secret
          }
        )
      when :phone
        SendSmsWorker.perform_async(
          to: identity.claim,
          body: "Your GroupFire verification code is: #{secret}."
        )
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
punk-0.3.6 lib/punk/services/challenge_claim.rb
punk-0.3.5 lib/punk/services/challenge_claim.rb
punk-0.3.4 lib/punk/services/challenge_claim.rb
punk-0.3.3 lib/punk/services/challenge_claim.rb
punk-0.3.2 lib/punk/services/challenge_claim.rb
punk-0.3.1 lib/punk/services/challenge_claim.rb
punk-0.2.0 lib/punk/services/challenge_claim.rb