Skip to content

Commit 141f74d

Browse files
committed
testing temp user
1 parent 8ea593e commit 141f74d

File tree

3 files changed

+104
-5
lines changed

3 files changed

+104
-5
lines changed

src/lavinmq/auth/authenticators/oauth.cr

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
require "../authenticator"
2+
require "../users/temp_user"
23
require "../../server"
34

45
module LavinMQ
56
module Auth
67
class OAuthAuthenticator < Authenticator
7-
def initialize(@users : UserStore)
8+
def initialize(@users : Auth::UserStore)
89
end
910

10-
def authenticate(username : String, password : Bytes) : User?
11-
pp "oauth"
12-
if user = @users[username]?
13-
return user
11+
def authenticate(username : String, password : Bytes) : Users::TempUser?
12+
13+
#Todo: send in JWT token and verify it,
14+
# parse that body and return a TempUser
15+
16+
if user = @users[username]? # instead: if token is valid
17+
temp_user = Users::TempUser.new
18+
temp_user.set_expiration(Time.utc + 3600.milliseconds) # Set expiration to 1 hour
19+
return temp_user
1420
end
21+
1522
rescue ex : Exception
1623
Log.error { "Oauth authentication failed: #{ex.message}" }
1724
end

src/lavinmq/auth/user_store.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ module LavinMQ
3737
user
3838
end
3939

40+
#TODO: create_temporary_user
41+
4042
def add(name, password_hash, password_algorithm, tags = Array(Tag).new, save = true)
4143
user = Users::BasicUser.new(name, password_hash, password_algorithm, tags)
4244
@users[name] = user

src/lavinmq/auth/users/temp_user.cr

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,99 @@
11
require "../user"
2+
require "../password"
3+
require "../../tag"
24

35
module LavinMQ
46
module Auth
57
module Users
68
class TempUser < User
9+
include SortableJSON
10+
11+
getter name, password, permissions
12+
property tags
13+
alias Permissions = NamedTuple(config: Regex, read: Regex, write: Regex)
14+
15+
@name : String
16+
@permissions = Hash(String, Permissions).new
17+
@password : Password? = nil
18+
@tags = Array(Tag).new
19+
@expiration_time : Time?
20+
21+
def initialize
22+
pp "hello"
23+
@name = "guest"
24+
@password = Password::MD5Password.create("guest")
25+
@tags = [Tag::Administrator]
26+
@permissions["/"] = {config: /.*/, read: /.*/, write: /.*/}
27+
@expiration_time = nil
28+
end
29+
30+
def set_expiration(time : Time)
31+
@expiration_time = time
32+
end
33+
34+
def expired? : Bool
35+
return false unless @expiration_time
36+
Time.utc > @expiration_time.not_nil!
37+
end
38+
39+
def can_write?(vhost, name) : Bool
40+
pp "hellooo"
41+
return false if expired?
42+
perm = permissions[vhost]?
43+
perm ? perm_match?(perm[:write], name) : false
44+
end
45+
46+
def can_read?(vhost, name) : Bool
47+
return false if expired?
48+
perm = permissions[vhost]?
49+
perm ? perm_match?(perm[:read], name) : false
50+
end
51+
52+
def can_config?(vhost, name) : Bool
53+
return false if expired?
54+
perm = permissions[vhost]?
55+
perm ? perm_match?(perm[:config], name) : false
56+
end
57+
58+
def can_impersonate?
59+
return false if expired?
60+
@tags.includes? Tag::Impersonator
61+
end
62+
63+
def hidden?
64+
false
65+
end
66+
67+
def details_tuple
68+
user_details.merge(permissions: @permissions)
69+
end
70+
71+
def user_details
72+
{
73+
name: @name,
74+
password_hash: @password,
75+
hashing_algorithm: @password.try &.hash_algorithm,
76+
tags: @tags.map(&.to_s.downcase).join(","),
77+
}
78+
end
79+
80+
def permissions_details
81+
@permissions.map { |k, p| permissions_details(k, p) }
82+
end
83+
84+
def permissions_details(vhost, p)
85+
{
86+
user: @name,
87+
vhost: vhost,
88+
configure: p[:config],
89+
read: p[:read],
90+
write: p[:write],
91+
}
92+
end
93+
94+
private def perm_match?(perm, name)
95+
perm != /^$/ && perm != // && perm.matches? name
96+
end
797
end
898
end
999
end

0 commit comments

Comments
 (0)