-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
oauth2: Introduce audience capabilities
This patch allows clients to whitelist audiences and request that audiences are set for oauth2 access and refresh tokens Closes #326 Signed-off-by: arekkas <aeneas@ory.am>
- Loading branch information
arekkas
committed
Oct 29, 2018
1 parent
799fc70
commit 891c4a8
Showing
47 changed files
with
485 additions
and
133 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package fosite | ||
|
||
import ( | ||
"github.com/ory/go-convenience/stringsx" | ||
"github.com/pkg/errors" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
type AudienceMatchingStrategy func(haystack []string, needle []string) error | ||
|
||
func DefaultAudienceMatchingStrategy(haystack []string, needle []string) error { | ||
if len(needle) == 0 { | ||
return nil | ||
} | ||
|
||
for _, n := range needle { | ||
nu, err := url.Parse(n) | ||
if err != nil { | ||
return errors.WithStack(ErrInvalidRequest.WithHintf(`Unable to parse requested audience "%s".`, n).WithDebug(err.Error())) | ||
} | ||
|
||
var found bool | ||
for _, h := range haystack { | ||
hu, err := url.Parse(h) | ||
if err != nil { | ||
return errors.WithStack(ErrInvalidRequest.WithHintf(`Unable to parse whitelisted audience "%s".`, h).WithDebug(err.Error())) | ||
} | ||
|
||
allowedPath := strings.TrimRight(hu.Path, "/") | ||
if nu.Scheme == hu.Scheme && | ||
nu.Host == hu.Host && | ||
( | ||
nu.Path == hu.Path || | ||
nu.Path == allowedPath || | ||
len(nu.Path) > len(allowedPath) && strings.TrimRight(nu.Path[:len(allowedPath)+1], "/") + "/" == allowedPath+"/") { | ||
found = true | ||
} | ||
} | ||
|
||
if !found { | ||
return errors.WithStack(ErrInvalidRequest.WithHintf(`Requested audience "%s" has not been whitelisted by the OAuth 2.0 Client.`, n)) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (f *Fosite) validateAuthorizeAudience(r *http.Request, request *AuthorizeRequest) (error) { | ||
audience := stringsx.Splitx(request.Form.Get("audience"), " ") | ||
|
||
if err := f.AudienceMatchingStrategy(request.Client.GetAudience(), audience); err != nil { | ||
return err | ||
} | ||
|
||
request.SetRequestedAudience(Arguments(audience)) | ||
return nil | ||
} |
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,95 @@ | ||
package fosite | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestDefaultAudienceMatchingStrategy(t *testing.T) { | ||
for k, tc := range []struct { | ||
h []string | ||
n []string | ||
err bool | ||
}{ | ||
{ | ||
h: []string{}, | ||
n: []string{}, | ||
err: false, | ||
}, | ||
{ | ||
h: []string{"http://foo/bar"}, | ||
n: []string{}, | ||
err: false, | ||
}, | ||
{ | ||
h: []string{}, | ||
n: []string{"http://foo/bar"}, | ||
err: true, | ||
}, | ||
{ | ||
h: []string{"https://cloud.ory.sh/api/users"}, | ||
n: []string{"https://cloud.ory.sh/api/users"}, | ||
err: false, | ||
}, | ||
{ | ||
h: []string{"https://cloud.ory.sh/api/users"}, | ||
n: []string{"https://cloud.ory.sh/api/users/"}, | ||
err: false, | ||
}, | ||
{ | ||
h: []string{"https://cloud.ory.sh/api/users/"}, | ||
n: []string{"https://cloud.ory.sh/api/users/"}, | ||
err: false, | ||
}, | ||
{ | ||
h: []string{"https://cloud.ory.sh/api/users/"}, | ||
n: []string{"https://cloud.ory.sh/api/users"}, | ||
err: false, | ||
}, | ||
{ | ||
h: []string{"https://cloud.ory.sh/api/users"}, | ||
n: []string{"https://cloud.ory.sh/api/users/1234"}, | ||
err: false, | ||
}, | ||
{ | ||
h: []string{"https://cloud.ory.sh/api/users"}, | ||
n: []string{"https://cloud.ory.sh/api/users", "https://cloud.ory.sh/api/users/", "https://cloud.ory.sh/api/users/1234"}, | ||
err: false, | ||
}, | ||
{ | ||
h: []string{"https://cloud.ory.sh/api/users", "https://cloud.ory.sh/api/tenants"}, | ||
n: []string{"https://cloud.ory.sh/api/users", "https://cloud.ory.sh/api/users/", "https://cloud.ory.sh/api/users/1234", "https://cloud.ory.sh/api/tenants"}, | ||
err: false, | ||
}, | ||
{ | ||
h: []string{"https://cloud.ory.sh/api/users"}, | ||
n: []string{"https://cloud.ory.sh/api/users1234"}, | ||
err: true, | ||
}, | ||
{ | ||
h: []string{"https://cloud.ory.sh/api/users"}, | ||
n: []string{"http://cloud.ory.sh/api/users"}, | ||
err: true, | ||
}, | ||
{ | ||
h: []string{"https://cloud.ory.sh/api/users"}, | ||
n: []string{"https://cloud.ory.sh:8000/api/users"}, | ||
err: true, | ||
}, | ||
{ | ||
h: []string{"https://cloud.ory.sh/api/users"}, | ||
n: []string{"https://cloud.ory.xyz/api/users"}, | ||
err: true, | ||
}, | ||
} { | ||
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) { | ||
err := DefaultAudienceMatchingStrategy(tc.h, tc.n) | ||
if tc.err { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.