-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
138 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,14 +7,16 @@ import ( | |
|
||
func NoAuthMiddleware() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
c.Set("user", pkg.UserInfo{ | ||
requestContext := GetRequestContext(c) | ||
requestContext.User = pkg.UserInfo{ | ||
Email: "[email protected]", | ||
ID: "johndoe", | ||
Name: "John Doe", | ||
Groups: []string{}, | ||
IP: c.RemoteIP(), | ||
Projects: []pkg.Project{{Name: "todo"}}, | ||
}) | ||
Projects: []pkg.Project{{Name: "todo", ID: "todooo"}}, | ||
} | ||
SetRequestContext(c, requestContext) | ||
c.Next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/inseefrlab/onyxia-api/pkg" | ||
) | ||
|
||
func ProjectResolver() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
requestContext := GetRequestContext(c) | ||
if len(requestContext.User.Projects) == 0 { | ||
c.AbortWithStatusJSON(500, gin.H{"error": fmt.Sprintf("User %s has no project", requestContext.User.ID)}) | ||
return | ||
} | ||
|
||
headerProject := c.GetHeader("ONYXIA-PROJECT") | ||
if headerProject == "" { | ||
requestContext.Project = requestContext.User.Projects[0] | ||
} else { | ||
var foundProject pkg.Project | ||
for _, project := range requestContext.User.Projects { | ||
if project.ID == headerProject { | ||
foundProject = project | ||
requestContext.Project = project | ||
} | ||
} | ||
if foundProject.ID == "" { | ||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Requested project not found"}) | ||
} | ||
} | ||
SetRequestContext(c, requestContext) | ||
c.Next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/inseefrlab/onyxia-api/internal/configuration" | ||
"github.com/inseefrlab/onyxia-api/pkg" | ||
) | ||
|
||
type RequestContext struct { | ||
User pkg.UserInfo | ||
Project pkg.Project | ||
Region configuration.Region | ||
} | ||
|
||
var requestContextKey = "requestContext" | ||
|
||
func GetRequestContext(c *gin.Context) RequestContext { | ||
context, exists := c.Get(requestContextKey) | ||
if !exists { | ||
context = RequestContext{} | ||
c.Set(requestContextKey, context) | ||
} | ||
return context.(RequestContext) | ||
} | ||
|
||
func SetRequestContext(c *gin.Context, newContext RequestContext) { | ||
c.Set(requestContextKey, newContext) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters