This commit is contained in:
+22
-2
@@ -36,8 +36,27 @@ type Manager struct {
|
||||
Client *http.Client
|
||||
}
|
||||
|
||||
type contextKey string
|
||||
|
||||
const UserIDKey contextKey = "user_id"
|
||||
|
||||
func UserID(r *http.Request) string {
|
||||
if v, ok := r.Context().Value(UserIDKey).(string); ok {
|
||||
return v
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func firstEnv(keys ...string) string {
|
||||
for _, k := range keys {
|
||||
if v := os.Getenv(k); v != "" {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
func New(ctx context.Context, db *sql.DB) (*Manager, error) {
|
||||
c := Config{os.Getenv("APP_BASE_URL"), os.Getenv("OIDC_ISSUER"), os.Getenv("OIDC_CLIENT_ID"), os.Getenv("OIDC_CLIENT_SECRET"), os.Getenv("SESSION_SECRET")}
|
||||
c := Config{firstEnv("APP_BASE_URL", "PUBLIC_URL"), firstEnv("OIDC_ISSUER", "POCKET_ID_ISSUER"), firstEnv("OIDC_CLIENT_ID", "POCKET_ID_CLIENT_ID"), firstEnv("OIDC_CLIENT_SECRET", "POCKET_ID_CLIENT_SECRET"), os.Getenv("SESSION_SECRET")}
|
||||
if c.SessionSecret == "" {
|
||||
c.SessionSecret = "dev-secret-change-me"
|
||||
}
|
||||
@@ -210,7 +229,8 @@ func (m *Manager) Require(next http.Handler) http.Handler {
|
||||
http.Redirect(w, r, "/login", 302)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
ctx := context.WithValue(r.Context(), UserIDKey, uid)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
func Logout(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user