Skip to content

consent: Add logout api endpoint#971

Closed
mderazon wants to merge 1 commit intoory:masterfrom
mderazon:logout-endpoint
Closed

consent: Add logout api endpoint#971
mderazon wants to merge 1 commit intoory:masterfrom
mderazon:logout-endpoint

Conversation

@mderazon
Copy link
Copy Markdown

@mderazon mderazon commented Aug 6, 2018

ory#970
Signed-off-by: Michael DeRazon <mderazon@gmail.com>
@mderazon
Copy link
Copy Markdown
Author

mderazon commented Aug 6, 2018

@arekkas the fix is very small, however since it's my first time dealing with Go, I was hitting some issues running tests (missing packages, even after go get) and also a weird behaviour something making changes to files in the project that I didn't ask for.

I checked and the changes were forced even after I closed VSC, very bizzare. No way to revert them, they just keep popping back.

Anyway, would probably take me a lot more time to understand the tooling around it than to actually make the changes.

Regarding the changes themselves:

  1. I am not sure I wrote the actual redirect correctly: 171fb6d#diff-ac4ce4ab0ca6c69035745a50d3108c65R603. Also, should it be 302 redirect ?
  2. Please review the swagger decorators for the route. Not sure it's good (see description and return type etc)
  3. Please review the route handler name. Naming is hard...
  4. Couldn't find a place to write test for this route.

Anyway, hope it's not creating more work than it would for you to write it :-/

Copy link
Copy Markdown
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

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

That's very weird (regarding the changes). I also use VSC but not extensively, primarily because the Go support is not as good as in GoLand. Not sure what's happening there but (as you did) just commit the relevant files.

Regarding to test, you actually need Docker installed and it's a bit slow. You can skip slow tests with go test -short ./... (no docker needed) which should work much better.

Regarding go get, you actually need Go's dependency management golang/dep (can't wait for Go 1.11 where this works natively):

go get -d -u github.com/ory/hydra
cd $(go env GOPATH)/src/github.com/ory/hydra
dep ensure -vendor-only
go test -short ./...

So regarding writing tests, this would be very cool! The idea would be to create a new file consent/handler_test.go and the first few lines should probably be something like this. Maybe set the redirect URL to something like https://www.ory.sh. Also make sure that ConsentStartegy is set (example here - I think you can leave everything empty/nil except for the cookiestore).

Then you create an HTTP client with a cookiejar:

cj, err := cookiejar.New()
require.NoErr(t, err)
client := http.Client{
  Jar: cj,
}

You would obviously need to set a cookie in the cookiejar because we are testing if the cookie is actually removed. Two options:

  1. Just add a cookie to the cookiejar with URL u, err := url.Parse(ts.URL) and a cookie that has the name of the authentication cookie (and some random value I think).
  2. I think you could t.Log("+v") the persistentCJ somewhere around here to get the data and mimic it in your test.

I'd try the first one before doing the second.

Then, once the request is done client.Get(ts.URL+"/oauth2/auth/logout") (I think), you should check for an error and then see if the status code is 200 and if yes, check if the url is http://www.ory.sh (the redirect url we set) and if that is also true, check if the cookie has been removed from the cookiejar.

If you have more questions let me know!

Comment thread consent/handler.go
})
}

// swagger:route DELETE /oauth2/auth/logout oAuth2 logoutUser
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's rename this to swagger:route GET /oauth2/auth/logout oAuth2 userLogout

Comment thread consent/handler.go

func (h *Handler) LogoutUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
err := revokeAuthenticationSession(w, r)
if err != nil {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perfect! You can actually write this in one line:

if err := revokeAuthenticationSession(w, r); err != nil {

Comment thread consent/handler.go
// listed in `LOGOUT_REDIRECT_URL` environment variable.
//
//
// Consumes:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can remove Consumes because we don't consume anything

Comment thread consent/handler.go
// Consumes:
// - text/html
//
// Produces:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It produces JSON in case of an error, no text/html

Comment thread consent/handler.go
return
}

http.Redirect(w, r, os.Getenv("LOGOUT_REDIRECT_URL"), 302)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This solution is ok, but it makes setting defaults and testing the configuration harder. Typically what we do here is:

  1. Add a config item in config/config.go
  2. Add a field (e.g. LogoutRedirectURL) to set this in the handler.go and the respective factors (new...)
  3. Bind and set a default in cmd/root.go like here.
  4. Use LogoutRedirectURL here instead of os.GetEnv()

Additionally, we should set up a default handler for the log out screen, similar to this one and add it to the frontend router here. The layout of the page doesn't matter. It should read something "You have been logged out successfully. ... Admin forgot to set up the redirect URL after logout ... to fix that set environment variable LogoutRedirectURL, e.g. export LogoutRedirectURL=http://... or on windows set LogoutRedirectURL=http://... ..."

Comment thread consent/handler.go
backend.DELETE("/oauth2/auth/sessions/consent/:user", h.DeleteUserConsentSession)
backend.DELETE("/oauth2/auth/sessions/consent/:user/:client", h.DeleteUserClientConsentSession)

frontend.GET("/oauth2/auth/logout", h.LogoutUser)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's rename this to UserLogout (because it's UserConsent :) )

@aeneasr
Copy link
Copy Markdown
Member

aeneasr commented Aug 7, 2018

One more thing, please document LOGOUT_REDIRECT_URL somewhere around here

@mderazon
Copy link
Copy Markdown
Author

mderazon commented Aug 7, 2018

Thanks for the valuable feedback.
One more thing - What should be the behaviour if the user does not set LOGOUT_REDIRECT_URL ?
Two options

  1. Fail at startup (too aggressive probably)
  2. Fail at the endpoint

@aeneasr
Copy link
Copy Markdown
Member

aeneasr commented Aug 7, 2018

One more thing - What should be the behaviour if the user does not set LOGOUT_REDIRECT_URL ?
Two options

See this comment: #971 (comment)

Additionally, we should set up a default handler for the log out screen, similar to this one and add it to the frontend router here. The layout of the page doesn't matter. It should read something "You have been logged out successfully. ... Admin forgot to set up the redirect URL after logout ... to fix that set environment variable LogoutRedirectURL, e.g. export LogoutRedirectURL=http://... or on windows set LogoutRedirectURL=http://... ..."

@aeneasr
Copy link
Copy Markdown
Member

aeneasr commented Aug 7, 2018

I know now why this (reformat) in the SDK happens. Apparently, gofmt tries to format the .php and .js files which causes this error (uppercasing). Not sure why it does that but that's the error. Happened to me as well. VScode executes gofmt automatically on all files

@mderazon
Copy link
Copy Markdown
Author

mderazon commented Aug 7, 2018

Were you able to disable it ?

It might be something with the repo, because if I clone the repo on a new location I am getting errors

git clone git@github.com:ory/hydra.git
Cloning into 'hydra'...
remote: Counting objects: 15217, done.
remote: Compressing objects: 100% (158/158), done.
remote: Total 15217 (delta 119), reused 128 (delta 80), pack-reused 14973
Receiving objects: 100% (15217/15217), 33.07 MiB | 952.00 KiB/s, done.
Resolving deltas: 100% (10042/10042), done.
error: unable to unlink old 'sdk/js/swagger/docs/JsonWebKey.md': No such file or directory
error: unable to unlink old 'sdk/js/swagger/docs/JsonWebKeySet.md': No such file or directory
error: unable to unlink old 'sdk/js/swagger/src/model/JsonWebKey.js': No such file or directory
error: unable to unlink old 'sdk/js/swagger/src/model/JsonWebKeySet.js': No such file or directory
error: unable to unlink old 'sdk/php/swagger/docs/Model/JsonWebKey.md': No such file or directory
error: unable to unlink old 'sdk/php/swagger/docs/Model/JsonWebKeySet.md': No such file or directory
error: unable to unlink old 'sdk/php/swagger/lib/Model/JsonWebKey.php': No such file or directory
error: unable to unlink old 'sdk/php/swagger/lib/Model/JsonWebKeySet.php': No such file or directory
fatal: unable to checkout working tree
warning: Clone succeeded, but checkout failed.
You can inspect what was checked out with 'git status'
and retry the checkout with 'git checkout -f HEAD'

@aeneasr
Copy link
Copy Markdown
Member

aeneasr commented Aug 7, 2018

It was really weird, I commited all the changes to a new branch, then switched back. Only then did they disappear. I honestly have no idea what's going on here...

@aeneasr
Copy link
Copy Markdown
Member

aeneasr commented Aug 8, 2018

Did you manage to get it fixed? Maybe it was a broken git index? I had the issue several times too. I finally fixed it though, I think it had something to do with lower/uppercase of the filename. If this blocks you from working on the PR let me know.

@aeneasr
Copy link
Copy Markdown
Member

aeneasr commented Aug 10, 2018

Just letting you know that I'll take your changes and integrate my feedback so this can be released with beta.8 today

@aeneasr
Copy link
Copy Markdown
Member

aeneasr commented Aug 10, 2018

Thank you for your hard work - new PR is #984

@aeneasr aeneasr closed this Aug 10, 2018
@mderazon
Copy link
Copy Markdown
Author

Thanks a lot of this @arekkas .
Sorry I wasn't able to contribute more, was in the midst of incorporating your changes but was hitting the git issues and then had to work on something else.

It seems your changes ended up being more substantial and you also added the tests, so I guess it's for the better ;-)

Thanks again for all the hard work for this really great open source jewel

@aeneasr
Copy link
Copy Markdown
Member

aeneasr commented Aug 16, 2018

No problem!

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.

2 participants