mirror of
https://github.com/fosrl/badger.git
synced 2025-05-13 05:40:39 +01:00
Merge pull request #4 from fosrl/dev
send access token and pass cookies
This commit is contained in:
commit
d5fd63a6cd
3 changed files with 27 additions and 3 deletions
|
@ -9,3 +9,4 @@ testData:
|
||||||
apiBaseUrl: http://localhost:3001/api/v1
|
apiBaseUrl: http://localhost:3001/api/v1
|
||||||
userSessionCookieName: p_session
|
userSessionCookieName: p_session
|
||||||
resourceSessionCookieName: p_resource_session
|
resourceSessionCookieName: p_resource_session
|
||||||
|
accessTokenQueryParam: p_token
|
||||||
|
|
|
@ -16,8 +16,9 @@ Badger requires the following configuration parameters to be specified in your [
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
apiBaseUrl: "http://localhost:3001/api/v1"
|
apiBaseUrl: "http://localhost:3001/api/v1"
|
||||||
userSessionCookieName: "session"
|
userSessionCookieName: "p_session"
|
||||||
resourceSessionCookieName: "resource_session"
|
resourceSessionCookieName: "p_resource_session"
|
||||||
|
accessTokenQueryParam: "p_token"
|
||||||
```
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
24
main.go
24
main.go
|
@ -13,6 +13,7 @@ type Config struct {
|
||||||
APIBaseUrl string `json:"apiBaseUrl"`
|
APIBaseUrl string `json:"apiBaseUrl"`
|
||||||
UserSessionCookieName string `json:"userSessionCookieName"`
|
UserSessionCookieName string `json:"userSessionCookieName"`
|
||||||
ResourceSessionCookieName string `json:"resourceSessionCookieName"`
|
ResourceSessionCookieName string `json:"resourceSessionCookieName"`
|
||||||
|
AccessTokenQueryParam string `json:"accessTokenQueryParam"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type VerifyBody struct {
|
type VerifyBody struct {
|
||||||
|
@ -22,6 +23,7 @@ type VerifyBody struct {
|
||||||
RequestHost *string `json:"host"`
|
RequestHost *string `json:"host"`
|
||||||
RequestPath *string `json:"path"`
|
RequestPath *string `json:"path"`
|
||||||
RequestMethod *string `json:"method"`
|
RequestMethod *string `json:"method"`
|
||||||
|
AccessToken *string `json:"accessToken,omitempty"`
|
||||||
TLS bool `json:"tls"`
|
TLS bool `json:"tls"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,6 +40,7 @@ type Badger struct {
|
||||||
apiBaseUrl string
|
apiBaseUrl string
|
||||||
userSessionCookieName string
|
userSessionCookieName string
|
||||||
resourceSessionCookieName string
|
resourceSessionCookieName string
|
||||||
|
accessTokenQueryParam string
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateConfig() *Config {
|
func CreateConfig() *Config {
|
||||||
|
@ -51,14 +54,27 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
|
||||||
apiBaseUrl: config.APIBaseUrl,
|
apiBaseUrl: config.APIBaseUrl,
|
||||||
userSessionCookieName: config.UserSessionCookieName,
|
userSessionCookieName: config.UserSessionCookieName,
|
||||||
resourceSessionCookieName: config.ResourceSessionCookieName,
|
resourceSessionCookieName: config.ResourceSessionCookieName,
|
||||||
|
accessTokenQueryParam: config.AccessTokenQueryParam,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Badger) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
func (p *Badger) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
cookies := p.extractCookies(req)
|
cookies := p.extractCookies(req)
|
||||||
|
|
||||||
|
var accessToken *string
|
||||||
|
queryValues := req.URL.Query()
|
||||||
|
if token := queryValues.Get(p.accessTokenQueryParam); token != "" {
|
||||||
|
accessToken = &token
|
||||||
|
queryValues.Del(p.accessTokenQueryParam)
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanedQuery := queryValues.Encode()
|
||||||
|
originalRequestURL := fmt.Sprintf("%s://%s%s", p.getScheme(req), req.Host, req.URL.Path)
|
||||||
|
if cleanedQuery != "" {
|
||||||
|
originalRequestURL = fmt.Sprintf("%s?%s", originalRequestURL, cleanedQuery)
|
||||||
|
}
|
||||||
|
|
||||||
verifyURL := fmt.Sprintf("%s/badger/verify-session", p.apiBaseUrl)
|
verifyURL := fmt.Sprintf("%s/badger/verify-session", p.apiBaseUrl)
|
||||||
originalRequestURL := fmt.Sprintf("%s://%s%s", p.getScheme(req), req.Host, req.URL.RequestURI())
|
|
||||||
|
|
||||||
cookieData := VerifyBody{
|
cookieData := VerifyBody{
|
||||||
Sessions: cookies,
|
Sessions: cookies,
|
||||||
|
@ -67,6 +83,7 @@ func (p *Badger) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
RequestHost: &req.Host,
|
RequestHost: &req.Host,
|
||||||
RequestPath: &req.URL.Path,
|
RequestPath: &req.URL.Path,
|
||||||
RequestMethod: &req.Method,
|
RequestMethod: &req.Method,
|
||||||
|
AccessToken: accessToken,
|
||||||
TLS: req.TLS != nil,
|
TLS: req.TLS != nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,6 +100,11 @@ func (p *Badger) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// pass through cookies
|
||||||
|
for _, setCookie := range resp.Header["Set-Cookie"] {
|
||||||
|
rw.Header().Add("Set-Cookie", setCookie)
|
||||||
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
http.Error(rw, "Internal Server Error", http.StatusInternalServerError)
|
http.Error(rw, "Internal Server Error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue