-
Notifications
You must be signed in to change notification settings - Fork 99
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
Poll for expired session #82
Merged
Merged
Changes from 24 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
887b717
Add fetch to tsconfig
jeffdaley 5712506
Fix type errors
jeffdaley b009947
Show reauthentication toast when logged out
jeffdaley b044898
Add test; add `basicTimeout`
jeffdaley dc76e7f
Set up basic 401 handler
jeffdaley 5d9790b
Move variable out of class
jeffdaley 04a7b82
Merge branch 'jeffdaley/redirect' into jeffdaley/google-pinger
jeffdaley 67da9fd
Update Mirage's `me` handling; Update tests
jeffdaley 047b776
Merge branch 'jeffdaley/fetch' into jeffdaley/google-pinger
jeffdaley f98893c
Add `isPollCall` argument to fetch
jeffdaley ca6014c
Add `me` to Mirage; Update tests
jeffdaley 891cc65
Add `me` to Mirage
jeffdaley 4de89da
Merge branch 'jeffdaley/mirage-me' of https://github.com/hashicorp-fo…
jeffdaley 124594b
Merge branch 'jeffdaley/mirage-me' into jeffdaley/google-pinger
jeffdaley aa7a9dd
Revert `create('me')` changes
jeffdaley 359916c
Merge branch 'main' into jeffdaley/google-pinger
jeffdaley 47d0509
Merge branch 'main' into jeffdaley/google-pinger
jeffdaley 09de392
Merge branch 'main' into jeffdaley/google-pinger
jeffdaley e00f8b1
Merge branch 'main' into jeffdaley/google-pinger
jeffdaley 644a2b1
Add /me endpoint to determine if the user is currently authenticated
jfreda 635feff
Revert stub endpoint
jeffdaley 051ab08
Merge branch 'main' into jeffdaley/google-pinger
jeffdaley 93f75b4
Merge remote-tracking branch 'origin/jfreda/add-me-endpoint' into jef…
jeffdaley 7ac24fe
Update endpoint
jeffdaley 8e907a9
Cleanup; documentation
jeffdaley 63f8272
Merge branch 'main' into jeffdaley/google-pinger
jeffdaley 513319f
Fix merge error
jeffdaley 7c778cb
Remove redundant headers
jeffdaley 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/hashicorp/go-hclog" | ||
) | ||
|
||
func MeHandler( | ||
l hclog.Logger, | ||
) http.Handler { | ||
|
||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
errResp := func(httpCode int, userErrMsg, logErrMsg string, err error) { | ||
l.Error(logErrMsg, | ||
"method", r.Method, | ||
"path", r.URL.Path, | ||
"error", err, | ||
) | ||
errJSON := fmt.Sprintf(`{"error": "%s"}`, userErrMsg) | ||
http.Error(w, errJSON, httpCode) | ||
} | ||
|
||
// Authorize request. | ||
userEmail := r.Context().Value("userEmail").(string) | ||
if userEmail == "" { | ||
errResp( | ||
http.StatusUnauthorized, | ||
"No authorization information for request", | ||
"no user email found in request context", | ||
nil, | ||
) | ||
return | ||
} | ||
|
||
switch r.Method { | ||
// The HEAD method is used to determine if the user is currently | ||
// authenticated. | ||
case "HEAD": | ||
w.WriteHeader(http.StatusOK) | ||
return | ||
|
||
default: | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
return | ||
} | ||
}) | ||
} |
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 was deleted.
Oops, something went wrong.
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,7 @@ | ||
import Component from "@glimmer/component"; | ||
import { inject as service } from "@ember/service"; | ||
import FlashMessageService from "ember-cli-flash/services/flash-messages"; | ||
|
||
export default class Notification extends Component { | ||
@service declare flashMessages: FlashMessageService; | ||
} |
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 |
---|---|---|
|
@@ -2,11 +2,79 @@ import { inject as service } from "@ember/service"; | |
import RouterService from "@ember/routing/router-service"; | ||
import EmberSimpleAuthSessionService from "ember-simple-auth/services/session"; | ||
import window from "ember-window-mock"; | ||
import { keepLatestTask } from "ember-concurrency"; | ||
import FlashMessageService from "ember-cli-flash/services/flash-messages"; | ||
import Ember from "ember"; | ||
import { tracked } from "@glimmer/tracking"; | ||
import simpleTimeout from "hermes/utils/simple-timeout"; | ||
import FetchService from "./fetch"; | ||
|
||
export const REDIRECT_LOCAL_STORAGE_KEY = "hermes.redirectTarget"; | ||
|
||
export default class SessionService extends EmberSimpleAuthSessionService { | ||
@service declare router: RouterService; | ||
@service declare fetch: FetchService; | ||
@service declare session: SessionService; | ||
@service declare flashMessages: FlashMessageService; | ||
|
||
/** | ||
* Whether the service should show a reauthentication message. | ||
* True when the user has dismissed a previous re-auth message. | ||
*/ | ||
@tracked preventReauthenticationMessage = false; | ||
|
||
@tracked tokenIsValid = true; | ||
@tracked pollResponseIs401 = false; | ||
|
||
/** | ||
* A persistent task that periodically checks if the user's | ||
* session has expired, and shows a flash message if it has. | ||
* Kicked off by the Authenticated route. | ||
*/ | ||
pollForExpiredAuth = keepLatestTask(async () => { | ||
await simpleTimeout(Ember.testing ? 100 : 10000); | ||
|
||
this.fetch.fetch( | ||
"/api/v1/me", | ||
{ | ||
method: "HEAD", | ||
headers: { | ||
"Hermes-Google-Access-Token": this.data.authenticated.access_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. Not strictly necessary because the fetch service adds this automatically for backend requests. |
||
}, | ||
true | ||
); | ||
|
||
let isLoggedIn = await this.requireAuthentication(null, () => {}); | ||
|
||
if (this.pollResponseIs401 || !isLoggedIn) { | ||
this.tokenIsValid = false; | ||
} | ||
|
||
if (this.tokenIsValid) { | ||
this.preventReauthenticationMessage = false; | ||
} else if (!this.preventReauthenticationMessage) { | ||
this.flashMessages.add({ | ||
title: "Login token expired", | ||
message: "Please reauthenticate to keep using Hermes.", | ||
type: "warning", | ||
sticky: true, | ||
destroyOnClick: false, | ||
preventDuplicates: true, | ||
buttonText: "Authenticate with Google", | ||
buttonIcon: "google", | ||
buttonAction: () => { | ||
this.authenticate("authenticator:torii", "google-oauth2-bearer"); | ||
this.flashMessages.clearMessages(); | ||
}, | ||
onDestroy: () => { | ||
this.preventReauthenticationMessage = true; | ||
}, | ||
}); | ||
} | ||
|
||
this.pollForExpiredAuth.perform(); | ||
}); | ||
|
||
// ember-simple-auth only uses a cookie to track redirect target if you're using fastboot, otherwise it keeps track of the redirect target as a parameter on the session service. See the source here: https://github.com/mainmatter/ember-simple-auth/blob/a7e583cf4d04d6ebc96b198a8fa6dde7445abf0e/packages/ember-simple-auth/addon/-internals/routing.js#L33-L50 | ||
// | ||
|
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 |
---|---|---|
@@ -1,24 +1,24 @@ | ||
.notifications-container { | ||
@apply fixed; | ||
z-index: 20; | ||
bottom: 8px; | ||
right: 8px; | ||
@apply fixed z-20 bottom-6 right-6; | ||
} | ||
|
||
.notification { | ||
// Animation example taken from: https://github.com/adopted-ember-addons/ember-cli-flash#animated-example | ||
opacity: 0; | ||
|
||
transition: all 700ms cubic-bezier(0.68, -0.55, 0.265, 1.55); | ||
margin: 16px; | ||
|
||
&.active { | ||
@keyframes notificationIn { | ||
from { | ||
opacity: 0; | ||
transform: translateX(8px); | ||
} | ||
to { | ||
opacity: 1; | ||
@apply left-2; | ||
transform: translateX(0); | ||
} | ||
} | ||
|
||
.notification { | ||
animation: notificationIn 700ms cubic-bezier(0.68, -0.55, 0.265, 1.55) | ||
forwards; | ||
|
||
&.exiting { | ||
opacity: 0; | ||
@apply left-0; | ||
} | ||
&.exiting { | ||
animation: notificationIn 300ms cubic-bezier(0.68, -0.55, 0.265, 1.55) | ||
reverse forwards; | ||
} | ||
} |
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,14 @@ | ||
/** | ||
* A timeout function for polling tasks. | ||
* Not registered with Ember's runloop | ||
* (unlike ember-concurrency's timeout helper), | ||
* so it doesn't hang in acceptance tests. | ||
* | ||
* See: https://ember-concurrency.com/docs/testing-debugging | ||
*/ | ||
|
||
export default function simpleTimeout(timeout: number) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, timeout); | ||
}); | ||
} |
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.
We used to pass in a no-op based on the now-outdated idea that
dismissOnClick
is always true.