-
Notifications
You must be signed in to change notification settings - Fork 380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add SlidingCookie plug #616
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
if Code.ensure_loaded?(Plug) do | ||
defmodule Guardian.Plug.SlidingCookie do | ||
@moduledoc """ | ||
WARNING! Use of this plug MAY allow a session to be maintained | ||
indefinitely without primary authentication by issuing new refresh | ||
tokens off the back of previous (still valid) tokens. Especially if your | ||
`resource_from_claims` implemention does not check resource validity (in | ||
a user database or whatever), you SHOULD then at least make such checks | ||
in the `sliding_cookie/3` implementation to make sure the resource still | ||
exists, is valid and permitted. | ||
|
||
Looks for a valid token in the request cookies, and replaces it, if: | ||
|
||
a. A valid unexpired token is found in the request cookies. | ||
b. There is a `:sliding_cookie` configuration (or plug option). | ||
c. The token age (since issue) exceeds that configuration. | ||
d. The implementation module `sliding_cookie/3` returns `{:ok, new_claims}`. | ||
|
||
Otherwise the plug does nothing. | ||
|
||
The implementation module MUST implement the `sliding_cookie/3` function | ||
if this plug is used. The return value, if an updated cookie is approved | ||
of, should be `{:ok, new_claims}`. The `sliding_cookie/3` function should | ||
take any security action (such as checking a database to check a user has | ||
not been disabled). Anything else returned will be taken as an indication | ||
that the cookie should not refreshed. | ||
|
||
The only case whereby the error handler is employed is if the | ||
`sliding_cookie/3` function is not provided, in which case it is called | ||
with a type of `:implementation_fault` and reason `:no_sliding_cookie_fn`. | ||
|
||
This, like all other Guardian plugs, requires a Guardian pipeline to be setup. | ||
It requires an implementation module, an error handler and a key. | ||
|
||
These can be set either: | ||
|
||
1. Upstream on the connection with `plug Guardian.Pipeline` | ||
2. Upstream on the connection with `Guardian.Pipeline.{put_module, put_error_handler, put_key}` | ||
3. Inline with an option of `:module`, `:error_handler`, `:key` | ||
|
||
Nothing is done with the token, refreshed or not, no errors are handled as validity and expiry | ||
can be checked by the VerifyCookie and EnsureAuthenticated plugs respectively. | ||
|
||
Options: | ||
|
||
* `:key` - The location of the token (default `:default`) | ||
* `:sliding_cookie` - The minimum TTL remaining after which a replacement will be issued. Defaults to configured values. | ||
* `:halt` - Whether to halt the connection in case of error. Defaults to `true`. | ||
|
||
The `:sliding_cookie` config (or plug option) should be the same format as `:ttl`, for example | ||
`{1, :hour}`, and obviously it should be less than the prevailing `:ttl`. | ||
""" | ||
|
||
import Plug.Conn | ||
import Guardian.Plug, only: [find_token_from_cookies: 2, maybe_halt: 2] | ||
|
||
alias Guardian.Plug.Pipeline | ||
|
||
import Guardian, only: [ttl_to_seconds: 1, decode_and_verify: 4, timestamp: 0] | ||
|
||
@behaviour Plug | ||
|
||
@impl Plug | ||
@spec init(opts :: Keyword.t()) :: Keyword.t() | ||
def init(opts), do: opts | ||
|
||
@impl Plug | ||
@spec call(conn :: Plug.Conn.t(), opts :: Keyword.t()) :: Plug.Conn.t() | ||
def call(%{req_cookies: %Plug.Conn.Unfetched{}} = conn, opts) do | ||
conn | ||
|> fetch_cookies() | ||
|> call(opts) | ||
end | ||
|
||
def call(conn, opts) do | ||
with {:ok, token} <- find_token_from_cookies(conn, opts), | ||
module <- Pipeline.fetch_module!(conn, opts), | ||
{:ok, ttl_softlimit} <- sliding_window(module, opts), | ||
{:ok, %{"exp" => exp} = claims} <- decode_and_verify(module, token, %{}, opts), | ||
{:ok, resource} <- module.resource_from_claims(claims), | ||
true <- timestamp() >= exp - ttl_softlimit, | ||
{:ok, new_c} <- module.sliding_cookie(claims, resource, opts) do | ||
conn | ||
|> Guardian.Plug.remember_me(module, resource, new_c) | ||
else | ||
{:error, :not_implemented} -> | ||
conn | ||
|> Pipeline.fetch_error_handler!(opts) | ||
|> apply(:auth_error, [conn, {:implementation_fault, :no_sliding_cookie_fn}, opts]) | ||
|> maybe_halt(opts) | ||
|
||
_ -> | ||
conn | ||
end | ||
end | ||
|
||
defp sliding_window(module, opts) do | ||
case Keyword.get(opts, :sliding_cookie, module.config(:sliding_cookie)) do | ||
nil -> | ||
:no_sliding_window | ||
|
||
ttl_descr -> | ||
{:ok, ttl_to_seconds(ttl_descr)} | ||
Hanspagh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might make sense to move this to https://github.com/ueberauth/guardian/blob/master/lib/guardian/token/jwt.ex and do a small refactor to avoid a bit of code dublication
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well.. it's funny you should say this, as a version of this logic did previously exist in jwt.ex, I necessarily decoupled the
Map.put
stuff inassign_exp_from_ttl
from the actual ttl parsing to avoid the duplication, and then moved it out of jwt.ex to guardian.ex as I viewed it as a general utility function; it didn't seem to have anything really to do with the JWT token implementation at all as such at that point... and calling code from jwt.ex to parse a config option just didn't seem quite right to me... I'll move it to jwt.ex if you like though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you are right, that it should actually live in
guardian
, and we should just refactorjwt
to use the code fromguardian
and not the other way around