blob: a84c674424d5e500cbc697db8e9a3f88b0bf7a8b [file]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package auth
import (
"errors"
"github.com/gin-gonic/gin"
)
const principalContextKey = "dubbo-admin.auth.principal"
var ErrNoPrincipal = errors.New("authenticated principal is missing")
type Principal struct {
Subject string `json:"subject"`
Username string `json:"username"`
Email string `json:"email"`
Groups []string `json:"groups"`
Roles []string `json:"roles"`
AuthType string `json:"authType"`
Provider string `json:"provider"`
}
func LocalPrincipal(username string) Principal {
return Principal{
Subject: "local:" + username,
Username: username,
Groups: []string{},
Roles: []string{},
AuthType: "password",
Provider: "local",
}
}
func PutPrincipalInContext(ctx *gin.Context, principal Principal) {
ctx.Set(principalContextKey, principal)
}
func PrincipalFromContext(ctx *gin.Context) (Principal, bool) {
value, ok := ctx.Get(principalContextKey)
if !ok {
return Principal{}, false
}
principal, ok := value.(Principal)
return principal, ok
}