Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
olevitt committed Mar 21, 2024
1 parent 8cb55a2 commit 7b6e463
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 39 deletions.
20 changes: 20 additions & 0 deletions cmd/authentication-none.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"github.com/gin-gonic/gin"
pkg "github.com/inseefrlab/onyxia-api/pkg"
)

func NoAuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("user", pkg.UserInfo{
Email: "[email protected]",
ID: "johndoe",
Name: "John Doe",
Groups: []string{},
IP: c.RemoteIP(),
Projects: []pkg.Project{{Name: "todo"}},
})
c.Next()
}
}
9 changes: 9 additions & 0 deletions cmd/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/coreos/go-oidc/v3/oidc"
"github.com/gin-gonic/gin"
pkg "github.com/inseefrlab/onyxia-api/pkg"
)

type Claims struct {
Expand All @@ -31,6 +32,14 @@ func AuthMiddleware(ctx context.Context, verifier *oidc.IDTokenVerifier) gin.Han
return
}
c.Set("claims", IDTokenClaims)
c.Set("user", pkg.UserInfo{
Email: IDTokenClaims.Email,
ID: IDTokenClaims.ID,
Name: IDTokenClaims.Name,
Groups: IDTokenClaims.Groups,
IP: c.RemoteIP(),
Projects: []pkg.Project{{Name: "todo"}},
})
}

c.Next()
Expand Down
42 changes: 3 additions & 39 deletions cmd/user-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,9 @@ import (
"net/http"

"github.com/gin-gonic/gin"
pkg "github.com/inseefrlab/onyxia-api/pkg"
)

type Project struct {
ID string `json:"id"`
Namespace string `json:"namespace"`
Name string `json:"name"`
VaultTopDir string `json:"vaultTopDir"`
}

type UserInfo struct {
Email string `json:"email,omitempty"`
ID string `json:"idep,omitempty"`
Name string `json:"nomComplet,omitempty"`
IP string `json:"ip,omitempty"`
Groups []string `json:"groups,omitempty"`
Projects []Project `json:"projects"`
}

// @Summary Get user info
// @Schemes
// @Description Get user info and projects
Expand All @@ -30,29 +15,8 @@ type UserInfo struct {
// @Success 200
// @Router /user/info [get]
func userInfo(c *gin.Context) {
claims, _ := c.Get("claims")
var userInfo UserInfo
if claims == nil {
userInfo = UserInfo{
Email: "[email protected]",
ID: "johndoe",
Name: "John Doe",
Groups: []string{},
IP: c.RemoteIP(),
Projects: []Project{{Name: "todo"}},
}
} else {
typedClaims := claims.(Claims)
userInfo = UserInfo{
Email: typedClaims.Email,
ID: typedClaims.ID,
Name: typedClaims.Name,
Groups: typedClaims.Groups,
IP: c.RemoteIP(),
Projects: []Project{{Name: "todo"}},
}
}
c.JSON(http.StatusOK, userInfo)
user, _ := c.Get("user")
c.JSON(http.StatusOK, user.(pkg.UserInfo))
}

func registerUserHandlers(r *gin.RouterGroup) {
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func main() {
r := gin.Default()
baseRoutes := r.Group(configuration.Config.RootPath)
docs.SwaggerInfo.Description = "Swagger"
docs.SwaggerInfo.BasePath = configuration.Config.RootPath
baseRoutes.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
privateRoutes := baseRoutes.Group("/")
publicRoutes := baseRoutes.Group("/public")
Expand All @@ -54,6 +55,8 @@ func main() {
}
verifier := provider.Verifier(oidcConfig)
privateRoutes.Use(cmd.AuthMiddleware(ctx, verifier))
} else {
privateRoutes.Use(cmd.NoAuthMiddleware())
}

kubernetes.InitClient()
Expand Down
17 changes: 17 additions & 0 deletions pkg/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package pkg

type Project struct {
ID string `json:"id"`
Namespace string `json:"namespace"`
Name string `json:"name"`
VaultTopDir string `json:"vaultTopDir"`
}

type UserInfo struct {
Email string `json:"email,omitempty"`
ID string `json:"idep,omitempty"`
Name string `json:"nomComplet,omitempty"`
IP string `json:"ip,omitempty"`
Groups []string `json:"groups,omitempty"`
Projects []Project `json:"projects"`
}

0 comments on commit 7b6e463

Please sign in to comment.