Class: SqAuth::SqAuthClient

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SqAuthClient

Returns a new instance of SqAuthClient.



4
5
6
7
8
9
10
11
12
13
# File 'lib/sq_auth/sq_auth_client.rb', line 4

def initialize options = {}
  @https = !!options[:https]
  @host = options[:host] || "localhost"
  @port = (options[:port] || (@https ? 443 : 80)).to_i
  @username = options[:username]
  @password = options[:password]
  @server = options[:server_type] || SqAuth::SqAuthServer::BasicServer
  @user = options[:user_type] || SqAuth::SqAuthUser::BasicUser.new
  @sessions = SqAuth::SqAuthSessions.new
end

Instance Method Details

#auth_server_uriObject



107
108
109
110
# File 'lib/sq_auth/sq_auth_client.rb', line 107

def auth_server_uri
  uri_builder = (@https ? URI::HTTPS : URI::HTTP)
  uri_builder.build(host: @host, port: @port)
end

#available?Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/sq_auth/sq_auth_client.rb', line 31

def available?
  response = check_connection
  response.is_a?(Hash) && response[:status] == "200"
end

#check_connectionObject



27
28
29
# File 'lib/sq_auth/sq_auth_client.rb', line 27

def check_connection
  send_request :check_connection
end

#create_session(user, session) ⇒ Object



56
57
58
# File 'lib/sq_auth/sq_auth_client.rb', line 56

def create_session user, session
  @sessions[user] = session
end

#create_session_for_current_user(session) ⇒ Object



15
16
17
# File 'lib/sq_auth/sq_auth_client.rb', line 15

def create_session_for_current_user session
  create_session current_user, session
end

#current_userObject



52
53
54
# File 'lib/sq_auth/sq_auth_client.rb', line 52

def current_user
  @user.current_user
end

#current_user_paramsObject



112
113
114
# File 'lib/sq_auth/sq_auth_client.rb', line 112

def current_user_params
  {session: session_for_current_user.session_value, ip: ip_for_current_user}
end

#delete_session(session) ⇒ Object



116
117
118
# File 'lib/sq_auth/sq_auth_client.rb', line 116

def delete_session session
  @sessions.delete_session session
end

#host_optionsObject



103
104
105
# File 'lib/sq_auth/sq_auth_client.rb', line 103

def host_options
  {host: @host, port: @port, username: @username, password: @password, https: @https}
end

#ip_for(user) ⇒ Object



64
65
66
# File 'lib/sq_auth/sq_auth_client.rb', line 64

def ip_for user
  user[:ip]
end

#ip_for_current_userObject



23
24
25
# File 'lib/sq_auth/sq_auth_client.rb', line 23

def ip_for_current_user
  ip_for current_user
end

#project_roles(user, project) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sq_auth/sq_auth_client.rb', line 78

def project_roles user, project
  session = session_for user
  session.project_roles(project) do
    request_hash = {sqauthsession: session.session_value, ip: user[:ip]}
    response = send_request :get_roles, request_hash
    ret = []
    if response.is_a?(Hash) && response[:data].is_a?(Array)
      ret = begin
        response[:data].map do |project_hash|
          if project_hash["project"] == project
            project_hash["role"]
          end
        end
      rescue => ex
        []
      end.flatten.compact.uniq
    end
    ret
  end
end

#role_exist?(user, roles, project) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
# File 'lib/sq_auth/sq_auth_client.rb', line 68

def role_exist? user, roles, project
  project_roles(user, project)
  session = session_for user
  session.role_exist?(roles, project) do
    request_hash = {sqauthsession: session.session_value, roles: [*roles], auth_name: project, ip: user[:ip]}
    response = send_request :check_role, request_hash
    response.is_a?(Hash) && response[:data].is_a?(Hash) && (response[:data]["role_exist"] == true)
  end
end

#role_exist_for_current_user?(role, project) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/sq_auth/sq_auth_client.rb', line 36

def role_exist_for_current_user? role, project
  role_exist? current_user, role, project
end

#roles_for_current_user(project) ⇒ Object



40
41
42
# File 'lib/sq_auth/sq_auth_client.rb', line 40

def roles_for_current_user project
  project_roles current_user, project
end

#send_request(request_name, params = nil) ⇒ Object



99
100
101
# File 'lib/sq_auth/sq_auth_client.rb', line 99

def send_request request_name, params = nil
  @server.send(request_name, params, host_options)
end

#session_for(user) ⇒ Object



60
61
62
# File 'lib/sq_auth/sq_auth_client.rb', line 60

def session_for user
  @sessions[user]
end

#session_for_current_userObject



19
20
21
# File 'lib/sq_auth/sq_auth_client.rb', line 19

def session_for_current_user
  session_for current_user
end

#userObject



44
45
46
# File 'lib/sq_auth/sq_auth_client.rb', line 44

def user
  @user.current_user
end

#username=(username) ⇒ Object



48
49
50
# File 'lib/sq_auth/sq_auth_client.rb', line 48

def username=(username)
  @username = username
end