Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
d87850e4
Commit
d87850e4
authored
9 years ago
by
Cam Saul
Browse files
Options
Downloads
Patches
Plain Diff
code cleanup
parent
26222907
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/metabase/api/session.clj
+28
-26
28 additions, 26 deletions
src/metabase/api/session.clj
with
28 additions
and
26 deletions
src/metabase/api/session.clj
+
28
−
26
View file @
d87850e4
...
...
@@ -28,13 +28,14 @@
;; The basic concept here is to keep a list of failed logins over the last hour. This list looks like:
;;
;; (["cam@metabase.com" #inst "2015-07-27T23:34:48.156-00:00"]
;; ["cam@metabase.com" #inst "2015-07-27T23:34:32.783-00:00"]
;; ["cam@metabase.com" #inst "2015-07-27T23:34:31.666-00:00"])
;; (["cam@metabase.com" 1438045261132]
;; ["cam@metabase.com" 1438045260450]
;; ["cam@metabase.com" 1438045259037]
;; ["cam@metabase.com" 1438045258204])
;;
;; Every time there's a failed login, push a new pair of [email timestamp] to the front of the list.
The list is thus
;; automatically ordered by date, and we can drop the portion of the list with failed logins that
are over an hour
;; old as needed.
;; Every time there's a failed login, push a new pair of [email timestamp
(milliseconds)
] to the front of the list.
;;
The list is thus
automatically ordered by date, and we can drop the portion of the list with failed logins that
;;
are over an hour
old as needed.
;;
;; Once a User has some number of failed login attempts over the past hour (e.g. 4), calculate some delay before
;; they're allowed to try to login again (e.g., 15 seconds). This number will increase exponentially as the number of
...
...
@@ -68,34 +69,35 @@
(
defn-
remove-old-failed-login-attempts
"Remove `failed-login-attempts` older than an hour."
[]
(
let
[
one-hour-ago
(
java.util.Date.
(
-
(
System/currentTimeMillis
)
(
*
6
0
60
1000
)
))
less-than-one-hour-old?
(
fn
[[
_
^
java.util.Date
date
]]
(
.after
date
one-hour-ago
))]
(
let
[
one-hour-ago
(
-
(
System/currentTimeMillis
)
(
*
100
0
60
60
))
less-than-one-hour-old?
(
fn
[[
_
timestamp
]]
(
>
timestamp
one-hour-ago
))]
(
reset!
failed-login-attempts
(
take-while
less-than-one-hour-old?
@
failed-login-attempts
))))
(
defn-
push-failed-login-attempt
"Record a failed login attempt. Add a new pair to `failed-login-attempts` for EMAIL."
[
email
]
{
:pre
[(
string?
email
)]}
;; First filter out old failed login attempts
(
remove-old-failed-login-attempts
)
;; Now push the new one to the front
(
swap!
failed-login-attempts
conj
[
email
(
java.util.Date.
)]))
(
remove-old-failed-login-attempts
)
; First filter out old failed login attempts
(
swap!
failed-login-attempts
conj
[
email
(
System/currentTimeMillis
)]))
; Now push the new one to the front
(
defn-
calculate-login-delay
"Calculate the appropriate delay (in seconds) before a user should be allowed to login again based on
MOST-RECENT-ATTEMPT and NUM-RECENT-ATTEMPTS. This function returns `nil` if there is no delay that should be required."
[
^
java.util.Date
most-recent-attempt
num-recent-attempts
]
(
when
most-recent-attempt
(
assert
(
=
(
type
most-recent-attempt
)
java.util.Date
))
[
most-recent-attempt-ms
num-recent-attempts
]
(
when
most-recent-attempt-ms
(
let
[
num-attempts-over-threshold
(
-
num-recent-attempts
failed-login-attempts-throttling-threshold
)]
(
when
(
>
num-attempts-over-threshold
0
)
(
let
[
delay-seconds
(
*
(
math/expt
num-attempts-over-threshold
failed-login-delay-exponent
)
failed-login-attempts-initial-delay-seconds
)
last-login+delay-ms
(
+
(
.getTime
most-recent-attempt
)
(
*
delay-seconds
1000
))
seconds-till-next-login
(
int
(
math/round
(
/
(
-
last-login+delay-ms
(
System/currentTimeMillis
))
1000
)))]
(
when
(
>
seconds-till-next-login
0
)
seconds-till-next-login
))))))
(
let
[
delay-ms
(
*
(
math/expt
num-attempts-over-threshold
failed-login-delay-exponent
)
failed-login-attempts-initial-delay-seconds
1000
)
next-login-allowed-at
(
+
most-recent-attempt-ms
delay-ms
)
ms-till-next-login
(
-
next-login-allowed-at
(
System/currentTimeMillis
))]
(
when
(
>
ms-till-next-login
0
)
;; convert to seconds
(
->
(
/
ms-till-next-login
1000
)
math/round
int
)))))))
(
defn-
check-throttle-login-attempts
"Throw an Exception if a User has tried (and failed) to log in too many times recently."
...
...
@@ -104,11 +106,11 @@
;; Remove any out-of-date failed login attempts
(
remove-old-failed-login-attempts
)
;; Now count the number of recent attempts with this email
(
let
[[[
_
most-recent-attempt
]
:as
recent-attempts
]
(
filter
(
fn
[[
attempt-email
_
]]
(
=
email
attempt-email
))
@
failed-login-attempts
)]
(
let
[[[
_
most-recent-attempt
-ms
]
:as
recent-attempts
]
(
filter
(
fn
[[
attempt-email
_
]]
(
=
email
attempt-email
))
@
failed-login-attempts
)]
(
println
"RECENT ATTEMPTS:\n"
(
metabase.util/pprint-to-str
'cyan
recent-attempts
))
;; TODO - remove debug logging
(
when-let
[
login-delay
(
calculate-login-delay
most-recent-attempt
(
count
recent-attempts
))]
(
when-let
[
login-delay
(
calculate-login-delay
most-recent-attempt
-ms
(
count
recent-attempts
))]
(
let
[
message
(
format
"Too many recent failed logins! You must wait %d seconds before trying again."
login-delay
)]
(
throw
(
ex-info
message
{
:status-code
400
:errors
{
:email
message
}}))))))
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment