Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,14 @@ In order to add a new oauth2 provider following input is required:
> needs the token now gets an identicon from the default avatar proxy, and a custom `AvatarSaver`
> that returns that fetch error fails the login with 500. The client also has a five-second timeout per fetch attempt.

> **Behaviour change for custom providers:** up to v1.30.1 and v2.6.1, the user info request could
> follow a redirect to another origin and sent the access token on each followed hop. Since the
> releases after those versions it
> refuses a redirect off the scheme, host and port of `InfoURL`, and the login fails with 503. An
> `http://` `InfoURL` that redirects to `https://` on the same host is one such case: write it with
> `https://`. When the user info of a host with non-ASCII characters answers with a redirect, write
> the host in its ASCII (`xn--`) form in `InfoURL` and in any absolute redirect target on that host.

2. Adds local oauth2 server user can fully customize. It uses [`github.com/go-oauth2/oauth2/v4`](https://github.com/go-oauth2/oauth2) library and example shows how to initialize the server (`initGoauth2Srv` in the [example](https://github.com/go-pkgz/auth/blob/master/_example/main.go)) and set up a provider (the `NewCustomServer` call in `main` there).
* to start local oauth2 server following options are used:
* `URL` - url of oauth2 server with port, required
Expand Down
31 changes: 29 additions & 2 deletions provider/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (p Oauth2Handler) AuthHandler(w http.ResponseWriter, r *http.Request) {
}

client := p.conf.Client(context.Background(), tok)
uinfo, err := client.Get(p.infoURL)
uinfo, err := p.infoClient(client).Get(p.infoURL)
if err != nil {
rest.SendErrorJSON(w, r, p.L, http.StatusServiceUnavailable, err, "failed to get client info")
return
Expand Down Expand Up @@ -292,6 +292,33 @@ func (p Oauth2Handler) AuthHandler(w http.ResponseWriter, r *http.Request) {
rest.RenderJSON(w, &u)
}

// infoClient returns a copy of the oauth2 client for the user info request. The oauth2 transport adds the
// access token to every redirect hop, so the copy stops at a redirect off the scheme, host and port of the
// user info URL, or past ten hops when the client has no redirect policy of its own, and returns that
// redirect response, which the caller answers with 503 and logs by status only, keeping a signed
// Location out of the log. The check runs after the client's own redirect policy, which may rewrite the
// request. With a non-ASCII info host no redirect is followed.
func (p Oauth2Handler) infoClient(authed *http.Client) *http.Client {
origin := ""
if info, err := url.Parse(p.infoURL); err == nil {
origin = urlOrigin(info)
}
ic := *authed
next := authed.CheckRedirect
ic.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if next != nil {
if err := next(req, via); err != nil {
return err
}
}
if (next == nil && len(via) >= 10) || origin == "" || urlOrigin(req.URL) != origin {
return http.ErrUseLastResponse
}
return nil
}
return &ic
}

// avatarClient returns the client the avatar saver fetches the user's picture with. It sends the access
// token only with requests to the scheme, host and port of the user info URL, so a picture served by the
// provider's own API, like the Microsoft Graph photo, still loads, while a picture on any other host,
Expand All @@ -304,7 +331,7 @@ func (p Oauth2Handler) avatarClient(authed *http.Client) *http.Client {
}
client := &http.Client{Timeout: 5 * time.Second, Transport: plain, CheckRedirect: authed.CheckRedirect}
info, err := url.Parse(p.infoURL)
if err != nil || authed.Transport == nil {
if err != nil {
return client
}
if origin := urlOrigin(info); origin != "" {
Expand Down
187 changes: 184 additions & 3 deletions provider/oauth2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http/cookiejar"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -824,7 +825,7 @@ func TestOauth2AvatarFetchSendsTokenOnlyToInfoHost(t *testing.T) {
cases := []struct {
name string
picture func(infoHost, otherHost string) string
want map[string][]string // "server path" to the Authorization headers it received
want map[string][]string
}{
{name: "picture on another host",
picture: func(_, other string) string { return other + "/pic" },
Expand Down Expand Up @@ -922,6 +923,187 @@ func TestOauth2AvatarFetchSendsTokenOnlyToInfoHost(t *testing.T) {
}
}

func TestOauth2InfoRequestRedirect(t *testing.T) {
const accessToken = "MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI3"
cases := []struct {
name string
infoPath string
wantStatus int
wantOther []string
}{
{name: "redirect to another origin is refused", infoPath: "/to-other", wantStatus: http.StatusServiceUnavailable},
{name: "redirect within the origin is followed", infoPath: "/to-same", wantStatus: http.StatusOK},
{name: "redirect past the hop cap is refused", infoPath: "/loop?n=0", wantStatus: http.StatusServiceUnavailable},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var mu sync.Mutex
var otherAuth []string
userJSON := `{"id":"myuser1","name":"blah"}`

other := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
otherAuth = append(otherAuth, r.Header.Get("Authorization"))
mu.Unlock()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_, err := w.Write([]byte(userJSON))
assert.NoError(t, err)
}))
defer other.Close()

login := httptest.NewUnstartedServer(nil)
defer login.Close()
loginURL := "http://" + login.Listener.Addr().String()

var sameAuth string
var oauthURL string
oauth := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/login/oauth/authorize":
http.Redirect(w, r, loginURL+"/callback?code=g0ZGZmNjVmOWI&state="+url.QueryEscape(r.URL.Query().Get("state")),
http.StatusFound)
case "/login/oauth/access_token":
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_, err := fmt.Fprintf(w, `{"access_token":%q,"token_type":"bearer"}`, accessToken)
assert.NoError(t, err)
case "/to-other":
http.Redirect(w, r, other.URL+"/user?sig=s3cr3tsig", http.StatusFound)
case "/loop":
n, err := strconv.Atoi(r.URL.Query().Get("n"))
require.NoError(t, err)
if n < 9 {
http.Redirect(w, r, fmt.Sprintf("%s/loop?n=%d", oauthURL, n+1), http.StatusFound)
return
}
http.Redirect(w, r, oauthURL+"/user?sig=s3cr3tsig", http.StatusFound)
case "/to-same":
http.Redirect(w, r, oauthURL+"/user", http.StatusFound)
case "/user":
mu.Lock()
sameAuth = r.Header.Get("Authorization")
mu.Unlock()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_, err := w.Write([]byte(userJSON))
assert.NoError(t, err)
default:
w.WriteHeader(http.StatusNotFound)
}
}))
defer oauth.Close()
oauthURL = oauth.URL

prov := Oauth2Handler{
name: "mock",
endpoint: oauth2.Endpoint{
AuthURL: oauth.URL + "/login/oauth/authorize",
TokenURL: oauth.URL + "/login/oauth/access_token",
},
infoURL: oauth.URL + c.infoPath,
mapUser: func(data UserData, _ []byte) token.User {
return token.User{ID: "mock_" + data.Value("id"), Name: data.Value("name")}
},
}
var logs strings.Builder
lg := logger.Func(func(format string, args ...any) {
mu.Lock()
defer mu.Unlock()
logs.WriteString(fmt.Sprintf(format, args...) + "\n")
})
jwtService := token.NewService(token.Opts{SecretReader: token.SecretFunc(mockKeyStore), SecureCookies: false,
TokenDuration: time.Hour, CookieDuration: days31})
prov = initOauth2Handler(Params{URL: loginURL, Cid: "cid", Csecret: "csecret", JwtService: jwtService,
Issuer: "remark42", L: lg}, prov)
login.Config.Handler = http.HandlerFunc(Service{Provider: prov}.Handler)
login.Start()

jar, err := cookiejar.New(nil)
require.NoError(t, err)
client := &http.Client{Jar: jar, Timeout: 5 * time.Second}
resp, err := client.Get(loginURL + "/login?site=remark")
require.NoError(t, err)
defer func() { assert.NoError(t, resp.Body.Close()) }()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, c.wantStatus, resp.StatusCode, "body %s", string(body))
assert.NotContains(t, string(body), "s3cr3tsig")

mu.Lock()
defer mu.Unlock()
assert.NotContains(t, logs.String(), "s3cr3tsig")
assert.Equal(t, c.wantOther, otherAuth)
if c.wantStatus != http.StatusOK {
assert.Empty(t, sameAuth, "the info url's own /user is never requested")
return
}
assert.Equal(t, "Bearer "+accessToken, sameAuth)
})
}
}

func TestOauth2InfoClientRedirectPolicy(t *testing.T) {
first := mustParseURL(t, "https://api.example.com/user")
req := func(raw string) *http.Request { return &http.Request{URL: mustParseURL(t, raw)} }
via := []*http.Request{{URL: first}}
tooMany := make([]*http.Request, 10)
for i := range tooMany {
tooMany[i] = &http.Request{URL: first}
}
var hookCalled bool
rewrite := func(r *http.Request, _ []*http.Request) error {
hookCalled = true
r.URL = mustParseURL(t, "https://evil.example.com/user")
return nil
}
moveFirst := func(_ *http.Request, v []*http.Request) error {
hookCalled = true
v[0].URL = mustParseURL(t, "https://evil.example.com/user")
return nil
}
errHook := errors.New("hook refused")
cases := []struct {
name string
infoURL string
hook func(*http.Request, []*http.Request) error
next *http.Request
via []*http.Request
wantErr error
wantHook bool
}{
{name: "same origin", next: req("https://API.example.com:443/v2/user"), via: via},
{name: "another host", next: req("https://evil.example.com/user"), via: via, wantErr: http.ErrUseLastResponse},
{name: "another scheme", next: req("http://api.example.com/user"), via: via, wantErr: http.ErrUseLastResponse},
{name: "non-ascii info host", infoURL: "https://äpi.example.com/user", next: req("https://äpi.example.com/me"),
via: via, wantErr: http.ErrUseLastResponse},
{name: "default cap", next: req("https://api.example.com/user"), via: tooMany, wantErr: http.ErrUseLastResponse},
{name: "hook error kept", hook: func(*http.Request, []*http.Request) error { return errHook },
next: req("https://api.example.com/user"), via: via, wantErr: errHook},
{name: "hook replaces the cap", hook: func(*http.Request, []*http.Request) error { return nil },
next: req("https://api.example.com/user"), via: tooMany},
{name: "origin checked after hook rewrite", hook: rewrite, next: req("https://api.example.com/user"), via: via,
wantErr: http.ErrUseLastResponse, wantHook: true},
{name: "origin from the info url, not from via", hook: moveFirst, next: req("https://evil.example.com/user"),
via: []*http.Request{{URL: first}}, wantErr: http.ErrUseLastResponse, wantHook: true},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
hookCalled = false
infoURL := c.infoURL
if infoURL == "" {
infoURL = first.String()
}
ic := Oauth2Handler{infoURL: infoURL}.infoClient(&http.Client{CheckRedirect: c.hook})
err := ic.CheckRedirect(c.next, c.via)
assert.True(t, err == c.wantErr, "got %v, want %v", err, c.wantErr)
assert.Equal(t, c.wantHook, hookCalled)
})
}

authed := &http.Client{}
Oauth2Handler{}.infoClient(authed)
assert.Nil(t, authed.CheckRedirect, "the oauth2 client itself keeps its redirect policy")
}

func TestURLOrigin(t *testing.T) {
cases := []struct{ a, b string }{
{"https://graph.microsoft.com/v1.0/me", "https://GRAPH.microsoft.com:443/beta/me/photo/$value"},
Expand Down Expand Up @@ -951,7 +1133,6 @@ func mustParseURL(t *testing.T, raw string) *url.URL {
return u
}

// fetchingAvatarSaver fetches the picture with the client it is given, like avatar.Proxy does
type fetchingAvatarSaver struct{}

func (fetchingAvatarSaver) Put(u token.User, client *http.Client) (avatarURL string, err error) {
Expand All @@ -971,7 +1152,7 @@ func TestOauth2AvatarClientTransport(t *testing.T) {
cases := []struct {
name string
infoURL string
want map[string]string // picture URL to the Authorization header the base transport receives
want map[string]string
}{
{name: "token only for the info origin", infoURL: "https://graph.microsoft.com/v1.0/me",
want: map[string]string{
Expand Down
31 changes: 29 additions & 2 deletions v2/provider/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (p Oauth2Handler) AuthHandler(w http.ResponseWriter, r *http.Request) {
}

client := p.conf.Client(context.Background(), tok)
uinfo, err := client.Get(p.infoURL)
uinfo, err := p.infoClient(client).Get(p.infoURL)
if err != nil {
rest.SendErrorJSON(w, r, p.L, http.StatusServiceUnavailable, err, "failed to get client info")
return
Expand Down Expand Up @@ -292,6 +292,33 @@ func (p Oauth2Handler) AuthHandler(w http.ResponseWriter, r *http.Request) {
rest.RenderJSON(w, &u)
}

// infoClient returns a copy of the oauth2 client for the user info request. The oauth2 transport adds the
// access token to every redirect hop, so the copy stops at a redirect off the scheme, host and port of the
// user info URL, or past ten hops when the client has no redirect policy of its own, and returns that
// redirect response, which the caller answers with 503 and logs by status only, keeping a signed
// Location out of the log. The check runs after the client's own redirect policy, which may rewrite the
// request. With a non-ASCII info host no redirect is followed.
func (p Oauth2Handler) infoClient(authed *http.Client) *http.Client {
origin := ""
if info, err := url.Parse(p.infoURL); err == nil {
origin = urlOrigin(info)
}
ic := *authed
next := authed.CheckRedirect
ic.CheckRedirect = func(req *http.Request, via []*http.Request) error {
if next != nil {
if err := next(req, via); err != nil {
return err
}
}
if (next == nil && len(via) >= 10) || origin == "" || urlOrigin(req.URL) != origin {
return http.ErrUseLastResponse
}
return nil
}
return &ic
}

// avatarClient returns the client the avatar saver fetches the user's picture with. It sends the access
// token only with requests to the scheme, host and port of the user info URL, so a picture served by the
// provider's own API, like the Microsoft Graph photo, still loads, while a picture on any other host,
Expand All @@ -304,7 +331,7 @@ func (p Oauth2Handler) avatarClient(authed *http.Client) *http.Client {
}
client := &http.Client{Timeout: 5 * time.Second, Transport: plain, CheckRedirect: authed.CheckRedirect}
info, err := url.Parse(p.infoURL)
if err != nil || authed.Transport == nil {
if err != nil {
return client
}
if origin := urlOrigin(info); origin != "" {
Expand Down
Loading
Loading