feat: Type-safe Sessions using TypeSafeMiddleware#48
Merged
Conversation
and statically associate CookieConfiguration with the user's type
Codecov Report
@@ Coverage Diff @@
## master #48 +/- ##
=========================================
- Coverage 70.54% 64.7% -5.84%
=========================================
Files 6 8 +2
Lines 258 357 +99
=========================================
+ Hits 182 231 +49
- Misses 76 126 +50
Continue to review full report at Codecov.
|
This was referenced Jun 1, 2018
Merged
ianpartridge
approved these changes
Jun 4, 2018
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Introduces a TypeSafeSession middleware which can be passed to a user's Codable Routing handler, and on which the user can define the data types that their application requires in the session.
The requirements of a TypeSafeSession type are:
final class. The class must befinalbecause the TypeSafeMiddleware protocol includes a static function with aSelftype reference. The guide will include an example of how to mutate the session in a handler if it is a struct (since it is passed in as a function argument which is aletconstant).When defining a new TypeSafeSession, the user must define:
sessionId: Stringpublic init(sessionId: String)that should construct a new, empty sessionstatic let sessionCookie: SessionCookiethat defines the cookie parameters (at minimum, the cookienameandsecret)static var store: Store?that defines how the session is persisted (if unspecified, defaults to an in-memory store).An example declaration:
Dependencies
This PR depends on Kitura/Kitura#1274 (TypeSafeMiddleware implementation).
Design and rationale
We decided to require the user to define the name of the session cookie as well as the secret.
The existing Session middleware implementation defines a default name of 'kitura-session-id', however it is expected to be a global middleware (a single Session used by all handlers).
By contrast, it would be reasonable for a user to define multiple TypeSafeSession types for different parts of their application. If they want to share the same cookie they can (by setting the cookie name to the same value), however they must also set the secret to the same value. Due to this requirement, we felt it was sensible to make the user think about both values.
We departed from the
CookieParameterarray, instead defining aSessionCookieinitializer with optional parameters. We felt this was more natural, and would otherwise have needed to extend the CookieParameter enum withnameanddomainfields. (domain was missing from the existing Session implementation).We did not define
HttpOnly, even though it is good practice, as it seems there is no way to set it independent ofsecurevia theNSHTTPCookieAPI.