Sha256: c948288e161efe2068e0b33cd613b961cf307f54919a7c09c2b9b9599c997d4c

Contents?: true

Size: 1.23 KB

Versions: 9

Compression:

Stored size: 1.23 KB

Contents

require 'yaml'
require 'core_ext/assert'

module Donjon
  class User
    attr_reader :name, :key, :repo

    def initialize(name:, key:, repo:)
      assert(key.n.num_bits == 2048)
      
      @name  = name
      @key   = key
      @repo  = repo
    end

    def save
      _path_for(@name, @repo).tap do |path|
        path.parent.mkpath
        path.write @key.public_key.to_pem
      end
      self
    end

    private

    module SharedMethods
      private 

      def _path_for(name, repo)
        repo.join("users/#{name}.pub")
      end
    end
    extend SharedMethods
    include SharedMethods

    module ClassMethods
      def find(name:, repo:)
        path = _path_for(name, repo)
        return unless path.exist?
        key = OpenSSL::PKey::RSA.new(path.read)
        new(name: name, key: key, repo: repo)
      end

      def each(repo, &block)
        container = repo.join('users')
        return unless container.exist?
        container.children.each do |child|
          next unless child.extname == '.pub'
          name = child.basename.to_s.chomp('.pub')
          key = OpenSSL::PKey::RSA.new(child.read)
          block.call new(name: name, key: key, repo: repo)
        end
      end
    end
    extend ClassMethods
  end



end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
donjon-2.0.1 lib/donjon/user.rb
donjon-2.0.0 lib/donjon/user.rb
donjon-1.0.1 lib/donjon/user.rb
donjon-1.0.0 lib/donjon/user.rb
donjon-0.0.5 lib/donjon/user.rb
donjon-0.0.4 lib/donjon/user.rb
donjon-0.0.3 lib/donjon/user.rb
donjon-0.0.2 lib/donjon/user.rb
donjon-0.0.1 lib/donjon/user.rb