Class: IONIS::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/ionisauthentication.rb

Instance Method Summary collapse

Constructor Details

#initialize(sshUserName, sshUserPass, forceUpdate = false) ⇒ Authentication

Instance of Authentication - sshUserName and sshUserPass are needed to update IONIS Auth. file from IONIS Server.

Parameters:

  • sshUserName (String)

    IONIS Login

  • sshUserPass (String)

    Unix password

  • forceUpdate (Boolean) (defaults to: false)

    Force update of IONIS Auth. files



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ionisauthentication.rb', line 18

def initialize sshUserName, sshUserPass, forceUpdate = false
  @tmpDir = File.join Dir.tmpdir, 'ionis_auth/'
  @lstStudent = Hash.new
  Dir.mkdir @tmpDir if !File.directory? @tmpDir
  if !File.exist? File.join(@tmpDir, 'ppp.blowfish') or forceUpdate == true
    begin
      Net::SCP.start('ssh.epitech.eu', sshUserName, :password => sshUserPass) do |scp|
        scp.download! '/afs/epitech.net/site/etc/ppp.blowfish', @tmpDir
        scp.download! '/afs/epitech.net/site/etc/passwd', @tmpDir
        scp.download! '/afs/epitech.net/site/etc/location', @tmpDir
      end
    rescue SocketError
      raise IONIS::Exception::HostNotFound.new 405, "Can't resolve ssh.epitech.eu"
    rescue Errno::ECONNREFUSED
      raise IONIS::Exception::ConnectionRefused.new 403, "Connection refused to ssh.epitech.eu"
    rescue Net::SSH::AuthenticationFailed
      raise IONIS::Exception::BadAuthentication.new 402, "Bad authentication"
    rescue Net::SCP::Error
      raise IONIS::Exception::FileGrabber.new 404, "Can't copy file from ssh.epitech.eu..."
    end
  end

  hFile = File.new((File.join @tmpDir, 'ppp.blowfish'), 'r')
  while line = hFile.gets
    line = line.split ' '
    @lstStudent[line[0]] = Hash.new
    if line[0]
      @lstStudent[line[0]][:login]= line[0]
      @lstStudent[line[0]][:password]= line[1]
      @lstStudent[line[0]][:picture]= "http://www.epitech.eu/intra/picture/#{line[0]}.jpg"
    end
  end
  hFile.close
  hFile = File.new((File.join @tmpDir, 'passwd'), 'r')
  while line = hFile.gets
    line = line.split ':'
    if @lstStudent[line[0]]
      @lstStudent[line[0]][:uid] = line[2]
      @lstStudent[line[0]][:gid] = line[3]
      if line[4].length > 0
        line[4] = line[4].split ' '
        line[4][0].capitalize!
        line[4][1].upcase!
        @lstStudent[line[0]][:fullname] = "#{line[4][0]} #{line[4][1]}"
      end
    end
  end
  hFile.close
  hFile = File.new((File.join @tmpDir, 'location'), 'r')
  while line = hFile.gets
    line = line.split ':'
    if @lstStudent[line[0]]
      @lstStudent[line[0]][:city] = line[1].strip
    end
  end
  hFile.close
end

Instance Method Details

#chkPass(userName, userPass) ⇒ Mixed

Check PPP Password of given user

Parameters:

  • userName (String)

    IONIS Login

  • userPass (String)

    PPP Password

Returns:

  • (Mixed)

    Informations about given user in Array if password if correct. Otherwize: false



80
81
82
83
84
# File 'lib/ionisauthentication.rb', line 80

def chkPass userName, userPass
  false if @lstStudent == nil
  return @lstStudent[userName] if BCrypt::Engine.hash_secret(userPass , @lstStudent[userName][:password]) == @lstStudent[userName][:password]
  false
end

#getUser(userName) ⇒ Hash

Get informations about one user

Parameters:

  • userName (String)

    IONIS Login

Returns:

  • (Hash)

    Information about given user in Array. If User doesn’t exist, the returned array will be empty



89
90
91
92
# File 'lib/ionisauthentication.rb', line 89

def getUser userName
  false if @lstStudent == nil
  return @lstStudent[userName]
end