Skip to content

Commit

Permalink
support setting uc openapi session (#6347)
Browse files Browse the repository at this point in the history
  • Loading branch information
chengjoey committed May 15, 2024
1 parent 026f7c0 commit d3625c4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 2 additions & 0 deletions cmd/erda-server/bootstrap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ openapi-auth-uc:
uc_redirect_addrs: "${SELF_PUBLIC_ADDR}"
session_cookie_name: "${SESSION_COOKIE_NAME:OPENAPISESSION}"
session_cookie_domain: "${COOKIE_DOMAIN}"
cookie_max_age: "${UC_COOKIE_MAX_AGE:7d}"
cookie_same_site: "${UC_COOKIE_SAME_SITE:2}"
openapi-auth-password:
_enable: ${UC_ENABLED:true}
weight: 50
Expand Down
11 changes: 8 additions & 3 deletions internal/core/openapi/openapi-ng/auth/uc-session/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,19 @@ func (p *provider) LoginCallback(rw http.ResponseWriter, r *http.Request) {
http.Error(rw, err.Error(), http.StatusUnauthorized)
return
}

http.SetCookie(rw, &http.Cookie{
cookie := &http.Cookie{
Name: p.Cfg.SessionCookieName,
Value: sessionID,
Domain: p.getSessionDomain(r.Host),
HttpOnly: true,
Secure: scheme == "https",
})
SameSite: http.SameSite(p.Cfg.CookieSameSite),
}
if p.Cfg.CookieMaxAge > 0 {
cookie.Expires = time.Now().Add(p.Cfg.CookieMaxAge)
}

http.SetCookie(rw, cookie)
http.Redirect(rw, r, referer, http.StatusFound)
}

Expand Down
18 changes: 11 additions & 7 deletions internal/core/openapi/openapi-ng/auth/uc-session/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"net/http"
"strings"
"time"

"github.com/go-redis/redis"

Expand All @@ -29,13 +30,16 @@ import (
)

type config struct {
Weight int64 `file:"weight" default:"100"`
RedirectAfterLogin string `file:"redirect_after_login"`
ClientID string `file:"client_id"`
UCAddr string `file:"uc_addr"`
UCRedirectAddrs []string `file:"uc_redirect_addrs"`
SessionCookieName string `file:"session_cookie_name"`
SessionCookieDomains []string `file:"session_cookie_domain"`
Weight int64 `file:"weight" default:"100"`
RedirectAfterLogin string `file:"redirect_after_login"`
ClientID string `file:"client_id"`
UCAddr string `file:"uc_addr"`
UCRedirectAddrs []string `file:"uc_redirect_addrs"`
SessionCookieName string `file:"session_cookie_name"`
SessionCookieDomains []string `file:"session_cookie_domain"`
CookieMaxAge time.Duration `file:"cookie_max_age" default:"24h" desc:"max age of the cookie. optional."`
// CookieSameSite default set to 2, which is `lax`, more options see https://github.com/golang/go/blob/619b419a4b1506bde1aa7e833898f2f67fd0e83e/src/net/http/cookie.go#L52-L57
CookieSameSite int `file:"cookie_same_site" default:"2" desc:"indicates if cookie is SameSite. optional."`
}

// +provider
Expand Down

0 comments on commit d3625c4

Please sign in to comment.