Skip to content

Commit 1b318a5

Browse files
committed
Add policy violation for too many devices
1 parent 54b1f0f commit 1b318a5

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

policies/authorization_grant/authorization_grant.rego

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,18 @@ violation contains {"msg": sprintf(
153153
)} if {
154154
common.requester_banned(input.requester, data.requester)
155155
}
156+
157+
violation contains {
158+
"code": "too-many-sessions",
159+
"msg": "user has too many active sessions"
160+
} if {
161+
# Only apply if session limits are enabled in the config
162+
data.session_limit != null
163+
# Only apply if it's a user logging in (who therefore has countable sessions)
164+
input.session_counts != null
165+
# For OAuth 2 login, a violation occurs when the soft limit has already been
166+
# reached or exceeded.
167+
# We use the soft limit because the user will be able to interactively remove
168+
# sessions to return under the limit.
169+
data.session_limit.soft_limit <= input.session_counts.total
170+
}

policies/authorization_grant/authorization_grant_test.rego

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,35 @@ test_mas_scopes if {
222222
with input.grant_type as "authorization_code"
223223
with input.scope as "urn:mas:admin"
224224
}
225+
226+
test_session_limiting if {
227+
authorization_grant.allow with input.user as user
228+
with input.session_counts as {"total": 1}
229+
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
230+
231+
authorization_grant.allow with input.user as user
232+
with input.session_counts as {"total": 31}
233+
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
234+
235+
not authorization_grant.allow with input.user as user
236+
with input.session_counts as {"total": 32}
237+
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
238+
239+
not authorization_grant.allow with input.user as user
240+
with input.session_counts as {"total": 42}
241+
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
242+
243+
not authorization_grant.allow with input.user as user
244+
with input.session_counts as {"total": 65}
245+
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
246+
247+
# No limit configured
248+
authorization_grant.allow with input.user as user
249+
with input.session_counts as {"total": 1}
250+
with data.session_limit as null
251+
252+
# Client credentials grant
253+
authorization_grant.allow with input.user as user
254+
with input.session_counts as null
255+
with data.session_limit as {"soft_limit": 32, "hard_limit": 64}
256+
}

0 commit comments

Comments
 (0)