-
-
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 all commits
8e03865
c787435
bf2d43d
bb13f87
fad4857
8711ff0
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
|
||
# 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 |
---|---|---|
|
@@ -37,6 +37,14 @@ def is_refresh_token_expired? | |
refresh_token_expires_at < Time.current | ||
end | ||
|
||
def revoke_api_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. Nice. |
||
update_columns(api_token_digest: nil) | ||
end | ||
|
||
def revoke_refresh_token | ||
update_columns(refresh_token_digest: nil) | ||
end | ||
|
||
private | ||
|
||
# Generate unique tokens and hashes them for secure db storage | ||
|
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))) | ||
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. Would this make hashed values of It should hash the current |
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,4 +100,22 @@ | |
expect(api_credential.refresh_token_digest).to eq(Digest::SHA256.hexdigest(refresh_token)) | ||
end | ||
end | ||
|
||
describe "#revoke_api_token" do | ||
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. 👍 |
||
it "sets api token to nil" do | ||
api_credential.return_new_api_token![:api_token] | ||
api_credential.revoke_api_token | ||
|
||
expect(api_credential.api_token_digest).to be_nil | ||
end | ||
end | ||
|
||
describe "#revoke_refresh_token" do | ||
it "sets refresh token to nil" do | ||
api_credential.return_new_refresh_token![:refresh_token] | ||
api_credential.revoke_refresh_token | ||
|
||
expect(api_credential.refresh_token_digest).to be_nil | ||
end | ||
end | ||
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
.