updates
This commit is contained in:
78
main.go
78
main.go
@@ -9,12 +9,30 @@ import (
|
||||
swaggerFiles "github.com/swaggo/files"
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
"gitlab.com/DeveloperDurp/DurpAPI/controller"
|
||||
_ "gitlab.com/DeveloperDurp/DurpAPI/docs"
|
||||
"gitlab.com/DeveloperDurp/DurpAPI/docs"
|
||||
"gitlab.com/DeveloperDurp/DurpAPI/model"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
var (
|
||||
host = model.Host
|
||||
version = model.Version
|
||||
Conf = &oauth2.Config{
|
||||
ClientID: model.ClientID,
|
||||
ClientSecret: model.ClientSecret,
|
||||
RedirectURL: model.RedirectURL,
|
||||
Scopes: []string{
|
||||
"email",
|
||||
"groups",
|
||||
},
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: model.AuthURL,
|
||||
TokenURL: model.TokenURL,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// @title DurpAPI
|
||||
// @version 1.0
|
||||
// @description API for Durp's needs
|
||||
// @termsOfService http://swagger.io/terms/
|
||||
|
||||
@@ -25,7 +43,6 @@ import (
|
||||
// @license.name Apache 2.0
|
||||
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
|
||||
// @host durpapi.durp.info
|
||||
// @BasePath /api/v1
|
||||
|
||||
// @securityDefinitions.apikey ApiKeyAuth
|
||||
@@ -36,20 +53,8 @@ func main() {
|
||||
|
||||
r := gin.Default()
|
||||
c := controller.NewController()
|
||||
var groups []string
|
||||
conf := &oauth2.Config{
|
||||
ClientID: c.ClientID,
|
||||
ClientSecret: c.ClientSecret,
|
||||
RedirectURL: c.RedirectURL,
|
||||
Scopes: []string{
|
||||
"email",
|
||||
"groups",
|
||||
},
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: c.AuthURL,
|
||||
TokenURL: c.TokenURL,
|
||||
},
|
||||
}
|
||||
docs.SwaggerInfo.Host = host
|
||||
docs.SwaggerInfo.Version = version
|
||||
|
||||
v1 := r.Group("/api/v1")
|
||||
{
|
||||
@@ -59,24 +64,24 @@ func main() {
|
||||
}
|
||||
token := v1.Group("/token")
|
||||
{
|
||||
token.GET("GenerateToken", c.GenerateToken(conf))
|
||||
token.GET("GenerateToken", c.GenerateToken(Conf))
|
||||
}
|
||||
openai := v1.Group("/openai")
|
||||
{
|
||||
groups = []string{"openai"}
|
||||
openai.Use(authMiddleware(groups))
|
||||
//groups = []string{"openai"}
|
||||
//openai.Use(authMiddleware())
|
||||
openai.GET("general", c.GeneralOpenAI)
|
||||
openai.GET("travelagent", c.TravelAgentOpenAI)
|
||||
}
|
||||
unraid := v1.Group("/unraid")
|
||||
{
|
||||
groups = []string{"unraid"}
|
||||
unraid.Use(authMiddleware(groups))
|
||||
//groups = []string{"unraid"}
|
||||
//unraid.Use(authMiddleware())
|
||||
unraid.GET("powerusage", c.UnraidPowerUsage)
|
||||
}
|
||||
}
|
||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
r.GET("/callback", CallbackHandler(conf))
|
||||
r.GET("/callback", CallbackHandler(Conf))
|
||||
|
||||
err := r.Run(":8080")
|
||||
if err != nil {
|
||||
@@ -84,33 +89,6 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func authMiddleware(groups []string) gin.HandlerFunc {
|
||||
|
||||
return func(c *gin.Context) {
|
||||
// Get the access token from the request header or query parameters
|
||||
accessToken := c.GetHeader("Authorization")
|
||||
if accessToken == "" {
|
||||
accessToken = c.Query("access_token")
|
||||
}
|
||||
|
||||
// Create an OAuth2 token from the access token
|
||||
token := &oauth2.Token{AccessToken: accessToken}
|
||||
|
||||
// Validate the token
|
||||
if !token.Valid() {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
// Add the token to the request context for later use
|
||||
//ctx := context.WithValue(c.Request.Context(), "token", token)
|
||||
//c.Request = c.Request.WithContext(ctx)
|
||||
|
||||
// Call the next handler
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// CallbackHandler receives the authorization code and exchanges it for a token
|
||||
func CallbackHandler(conf *oauth2.Config) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user