-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
oauth2: Enable client specific CORS settings (#1009)
Field `allowed_cors_origins` was added to OAuth 2.0 Clients. It enables CORS for the whitelisted URLS for paths which clients interact with, such as /oauth2/token. Closes #975 Signed-off-by: arekkas <aeneas@ory.am>
- Loading branch information
Showing
17 changed files
with
368 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,92 @@ | ||
/* | ||
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* @author Aeneas Rekkas <aeneas+oss@aeneas.io> | ||
* @Copyright 2017-2018 Aeneas Rekkas <aeneas+oss@aeneas.io> | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
package server | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/aeneasr/cors" | ||
"github.com/ory/fosite" | ||
"github.com/ory/go-convenience/corsx" | ||
"github.com/ory/go-convenience/stringslice" | ||
"github.com/ory/hydra/client" | ||
"github.com/ory/hydra/config" | ||
"github.com/ory/hydra/oauth2" | ||
) | ||
|
||
func newCORSMiddleware( | ||
enable bool, c *config.Config, | ||
o func(ctx context.Context, token string, tokenType fosite.TokenType, session fosite.Session, scope ...string) (fosite.TokenType, fosite.AccessRequester, error), | ||
clm func(id string) (*client.Client, error), | ||
) func(h http.Handler) http.Handler { | ||
if !enable { | ||
return func(h http.Handler) http.Handler { | ||
return h | ||
} | ||
} | ||
|
||
c.GetLogger().Info("Enabled CORS") | ||
po := corsx.ParseOptions() | ||
options := cors.Options{ | ||
AllowedOrigins: po.AllowedOrigins, | ||
AllowedMethods: po.AllowedMethods, | ||
AllowedHeaders: po.AllowedHeaders, | ||
ExposedHeaders: po.ExposedHeaders, | ||
MaxAge: po.MaxAge, | ||
AllowCredentials: po.AllowCredentials, | ||
OptionsPassthrough: po.OptionsPassthrough, | ||
Debug: po.Debug, | ||
AllowOriginRequestFunc: func(r *http.Request, origin string) bool { | ||
if stringslice.Has(po.AllowedOrigins, origin) { | ||
return true | ||
} | ||
|
||
username, _, ok := r.BasicAuth() | ||
if !ok || username == "" { | ||
token := fosite.AccessTokenFromRequest(r) | ||
if token == "" { | ||
return false | ||
} | ||
|
||
session := oauth2.NewSession("") | ||
_, ar, err := o(context.Background(), token, fosite.AccessToken, session) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
username = ar.GetClient().GetID() | ||
} | ||
|
||
cl, err := clm(username) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
if stringslice.Has(cl.AllowedCORSOrigins, origin) { | ||
return true | ||
} | ||
|
||
return false | ||
}, | ||
} | ||
return cors.New(options).Handler | ||
} |
Oops, something went wrong.