Skip to content
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

Browser cookie sessions #1178

Merged
merged 17 commits into from
Mar 22, 2024
Merged

Browser cookie sessions #1178

merged 17 commits into from
Mar 22, 2024

Conversation

ale8k
Copy link
Contributor

@ale8k ale8k commented Mar 18, 2024

Description

Implements browser sessions via cookies and persistent postgres storage. Also includes the facade method LoginWithSessionCookie.

Engineering checklist

Check only items that apply

  • Documentation updated
  • Covered by unit tests
  • Covered by integration tests

Test instructions

Notes for code reviewers

Copy link
Contributor

@kian99 kian99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really good, left a bunch of comments but I think it's basically ready.

@@ -84,6 +119,14 @@ func NewAuthenticationService(ctx context.Context, params AuthenticationServiceP
return nil, errors.E(op, errors.CodeServerConfiguration, err, "failed to create oidc provider")
}

if params.SessionCookieMaxAge == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought but could an age of 0 maybe be useful if we want sessions or cookies that only last the lifetime of a session? For whatever reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a little ready I found another stackoverflow post:

Setting setMaxAge to 0 will delete the cookie. Setting it to -1 will preserve it until the browser is closed.

Not sure if this is 100%, but think we'll need to use 0 for deletion. So will remove this check.

internal/auth/oauth2.go Show resolved Hide resolved
internal/auth/oauth2.go Outdated Show resolved Hide resolved
internal/auth/oauth2.go Outdated Show resolved Hide resolved
internal/auth/oauth2.go Outdated Show resolved Hide resolved
internal/jimmhttp/websocket.go Show resolved Hide resolved
internal/jimmtest/auth.go Outdated Show resolved Hide resolved
internal/jujuapi/admin_test.go Outdated Show resolved Hide resolved
internal/jujuapi/admin_test.go Outdated Show resolved Hide resolved
internal/jujuapi/controllerroot.go Outdated Show resolved Hide resolved
cmd/jimmsrv/main.go Outdated Show resolved Hide resolved
internal/auth/oauth2.go Outdated Show resolved Hide resolved
internal/auth/oauth2.go Show resolved Hide resolved
internal/auth/oauth2.go Show resolved Hide resolved
internal/auth/oauth2.go Outdated Show resolved Hide resolved
internal/jimmhttp/websocket.go Show resolved Hide resolved
internal/jimmhttp/websocket.go Outdated Show resolved Hide resolved
internal/jimmtest/auth.go Outdated Show resolved Hide resolved
internal/jujuapi/admin.go Outdated Show resolved Hide resolved
internal/jujuapi/admin.go Show resolved Hide resolved
@alesstimec
Copy link
Collaborator

Remove WIP from the PR title and commit messages and refactor the modifySession method, then i'll LGTM it :)

@ale8k ale8k changed the title WIP browser cookie sessions Browser cookie sessions Mar 21, 2024
Copy link
Collaborator

@alesstimec alesstimec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, right then.. this LGTM

Copy link
Member

@babakks babakks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. Just a few comments/suggestions.

}
if sessionCookieExpiryInt < 0 {
return errors.E("jimm session cookie expiry cannot be less than 0")
if sessionCookieMaxAgeInt < 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about zero itself? Shouldn't this be <= 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess if someone silly enough to put 0 and turn cookies off entirely it's kinda their own fault? :D

internal/auth/oauth2.go Show resolved Hide resolved
internal/auth/oauth2.go Show resolved Hide resolved
internal/auth/oauth2.go Show resolved Hide resolved
internal/auth/oauth2.go Show resolved Hide resolved
internal/auth/oauth2.go Show resolved Hide resolved
internal/auth/oauth2.go Show resolved Hide resolved
internal/auth/oauth2_test.go Show resolved Hide resolved
@ale8k ale8k merged commit efbf21c into canonical:feature-oidc Mar 22, 2024
4 checks passed
@@ -125,13 +125,13 @@ func start(ctx context.Context, s *service.Service) error {
secureSessionCookies = true
}

sessionCookieExpiry := os.Getenv("JIMM_SESSION_COOKIE_EXPIRY")
sessionCookieExpiryInt, err := strconv.Atoi(sessionCookieExpiry)
sessionCookieMaxAge := os.Getenv("JIMM_SESSION_COOKIE_MAX_AGE")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see we're updating config but not updating the charms. Can we make a card to "Update JIMM charms with new config options"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!

return context.WithValue(ctx, sessionIdentityContextKey{}, sessionIdentityId)
}

// SessionIdentityFromContext returns the session identity key from the context.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to ignore this one, just being pendantic - "returns the session identity value" right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep you're right


if authErr != nil {
zapctx.Error(ctx, "browser authentication error", zap.Any("err", authErr), zap.Stack("stack"))
writeInternalServerErrorClosure(ctx, conn, authErr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, why are we opting to return a websocket error instead of an HTTP error if we get an authErr?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can explain in call as other comment says

}

if h.Server == nil {
writeNormalClosure(ctx, conn)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If h.Server == nil doesn't that constitute an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can explain this in call as had some issues basically with websocket tests

// we need a dial websocket for juju containing a custom cookie jar to send cookies to
// a new server url when testing LoginWithSessionCookie. As such this closure simply
// passes the jar through.
func getDialWebsocketWithCustomCookieJar(jar *cookiejar.Jar) func(ctx context.Context, urlStr string, tlsConfig *tls.Config, ipAddr string) (jsoncodec.JSONConn, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants