-
-
Notifications
You must be signed in to change notification settings - Fork 487
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
Feature/6222 - Endpoint /api/v1/users/sign_out revokes access token and refresh token on request #6241
base: main
Are you sure you want to change the base?
Feature/6222 - Endpoint /api/v1/users/sign_out revokes access token and refresh token on request #6241
Changes from 6 commits
8e03865
c787435
bf2d43d
bb13f87
fad4857
8711ff0
51864b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,22 @@ def create | |
end | ||
end | ||
|
||
def destroy | ||
# fetch refresh token from request header | ||
refresh_token = request.headers["Authorization"]&.split(" ")&.last | ||
# find user's api credentials by refresh token | ||
api_credential = ApiCredential.find_by(refresh_token_digest: Digest::SHA256.hexdigest(refresh_token)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. api_credential = ApiCredential searches user based on
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if we should use the api_token for checking because it expires every 7 hours. Using the refresh token is more reliable because it has a longer lifespan. If we want an additional check, we can pass along the user id in the request body as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, traditionally though, it's the This goes beyond just But if we're going to sign the user out based on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I see. That makes sense now. |
||
# set api and refresh tokens to nil; otherwise render 401 | ||
if api_credential | ||
api_credential.revoke_api_token | ||
api_credential.revoke_refresh_token | ||
render json: {message: "Signed out successfully."}, status: 200 | ||
else | ||
render json: {message: "An error occured when signing out."}, status: 401 | ||
nil | ||
end | ||
end | ||
|
||
private | ||
|
||
def user_params | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
ApiCredential.destroy_all | ||
users = User.all | ||
|
||
users.each do |user| | ||
ApiCredential.create!(user: user, api_token_digest: Digest::SHA256.hexdigest(SecureRandom.hex(18)), refresh_token_digest: Digest::SHA256.hexdigest(SecureRandom.hex(18))) | ||
7riumph marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end |
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.
Consider how we are structuring the response from the iOS app. If we're using the header, it's the access token or "api_token" that would go there, while the refresh token is in the body.
You'll need
both
in order to check whose tokens torevoke
or set tonil
onsign_out
.