|
- /* ******************************************************************************
- * 2019 - present Contributed by Apulis Technology (Shenzhen) Co. LTD
- *
- * This program and the accompanying materials are made available under the
- * terms of the MIT License, which is available at
- * https://www.opensource.org/licenses/MIT
- *
- * See the NOTICE file distributed with this work for additional
- * information regarding copyright ownership.
- * 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.
- *
- * SPDX-License-Identifier: MIT
- ******************************************************************************/
- package middlewares
-
- import (
- "errors"
-
- "github.com/apulis/app/ai-arts-backend/internal/configs"
- "github.com/apulis/app/ai-arts-backend/internal/loggers"
- "github.com/apulis/go-business/pkg/jwt"
- "github.com/gin-gonic/gin"
- )
-
- var (
- userIdCtxKey = "userId"
- userNameCtxKey = "userName"
- groupIdCtxKey = "groupId"
- groupNameCtxKey = "groupAccount"
- orgIdCtxKey = "orgId"
- orgNameCtxKey = "orgName"
- )
-
- func AuthMiddleware() gin.HandlerFunc {
- appConfig := configs.GetAppConfig()
- return jwt.NewJwtAuthN(
- jwt.SigningAlgorithm(appConfig.Jwt.SignAlgorithm),
- jwt.SecretKey([]byte(appConfig.Jwt.SecretKey)),
- jwt.PublicKey(appConfig.Jwt.PublicKey),
- ).Middleware()
- }
-
- func FakeAuthMiddleware() gin.HandlerFunc {
- return func(c *gin.Context) {
- c.Set(userIdCtxKey, int64(666))
- c.Set(userNameCtxKey, "fakeUser")
- c.Set(groupIdCtxKey, int64(66))
- c.Set(groupNameCtxKey, "fakeGroup")
- c.Set(orgIdCtxKey, int64(6))
- c.Set(orgNameCtxKey, "fakeOrg")
- c.Next()
- }
- }
-
- func Mock() gin.HandlerFunc {
- return func(c *gin.Context) {
- for _, h := range c.Request.Header {
- loggers.GetLogger().Info(h)
- }
-
- }
- }
-
- // UserId get user id from the context. REQUIRES Middleware to have run.
- func UserId(ctx *gin.Context) (int64, error) {
- raw := ctx.GetInt64(userIdCtxKey)
-
- if raw == 0 {
- err := errors.New("userId does not exists in gin.Context")
- return 0, err
- }
-
- return raw, nil
- }
-
- // UserName get user name from the context. REQUIRES Middleware to have run.
- func UserName(ctx *gin.Context) string {
- return ctx.GetString(userNameCtxKey)
- }
-
- // GroupId get user group id from the context. REQUIRES Middleware to have run.
- func GroupId(ctx *gin.Context) (int64, error) {
- raw := ctx.GetInt64(groupIdCtxKey)
-
- // if raw == 0 {
- // err := errors.New("user group id does not exists in gin.Context")
- // return 0, err
- // }
-
- return raw, nil
- }
-
- // GroupName get user group name from the context. REQUIRES Middleware to have run.
- func GroupName(ctx *gin.Context) string {
- return ctx.GetString(groupNameCtxKey)
- }
-
- // OrgId get organization id from the context. REQUIRES Middleware to have run.
- func OrgId(ctx *gin.Context) (int64, error) {
- raw := ctx.GetInt64(orgIdCtxKey)
-
- /*
- 系统管理员不属于组织,所以他的组织id就是为0
- if raw == 0 {
- err := errors.New("orgId does not exists in gin.Context")
- return 0, err
- }
- */
-
- return raw, nil
- }
-
- // OrgName get org name from the context. REQUIRES Middleware to have run.
- func OrgName(ctx *gin.Context) string {
- return ctx.GetString(orgNameCtxKey)
- }
-
- func AutoAuthMiddleware() gin.HandlerFunc {
- appConfig := configs.GetAppConfig()
- loggers.GetLogger().Info("AutoAuthMiddleware switch:", appConfig.Jwt.Debug)
- if appConfig.Jwt.Debug {
- return FakeAuthMiddleware()
- }
- return AuthMiddleware()
- }
|