Sha256: 2d9a5207c7d9a1635bd6a8de12a648d6ca97d3913c5e8ea829e47dc921e68747

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module Helpers
  module Auth
    attr_accessor :current_scope
    
    def enterprise_protected!(domain = nil)
      return if admin_authorized?
      return if enterprise_authorized?(domain)

      redirect ENV['JWT_URL']
    end

    def enterprise_authorized?(domain)
      payload, _args = JWT.decode(
        token,
        ENV['JWT_HMAC_SECRET'],
        true,
        { algorithm: 'HS256' },
      )

      @current_scope = payload['scope']

      true
    rescue JWT::DecodeError
      false
    end

    def admin_protected!
      return if admin_authorized?

      redirect ENV['JWT_URL']
    end

    def admin_authorized?
      payload, _args = JWT.decode(
        token,
        ENV['JWT_HMAC_SECRET'],
        true,
        { algorithm: 'HS256' },
      )

      if payload['scope'] == 'admin'
        @current_scope = :admin
        return true
      end

      false
    rescue JWT::DecodeError
      false
    end

    def token
      request.env['admin_token'] || session['admin_token'] || request['admin_token']
    end

    def chomp_token
      return unless request['admin_token'].present?

      session['admin_token'] = request['admin_token']

      return if request.post?

      redirect request.path
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
osso-0.0.3.3 lib/osso/helpers/auth.rb
osso-0.0.3.1 lib/osso/helpers/auth.rb
osso-0.0.3 lib/osso/helpers/auth.rb
osso-0.0.2.10 lib/osso/helpers/auth.rb
osso-0.0.2.9 lib/osso/helpers/auth.rb
osso-0.0.2.8 lib/osso/helpers/auth.rb