feat: Added auth token server

issue #96
This commit is contained in:
Andrey Melnikov
2020-03-10 12:53:13 -07:00
parent 8a3306f4df
commit c5975a4022
9 changed files with 174 additions and 216 deletions

43
server/auth_server.go Normal file
View File

@@ -0,0 +1,43 @@
package server
import (
"context"
"github.com/onepanelio/core/api"
v1 "github.com/onepanelio/core/pkg"
"github.com/onepanelio/core/server/auth"
"github.com/pkg/errors"
)
type AuthServer struct{}
func NewAuthServer() *AuthServer {
return &AuthServer{}
}
func (a *AuthServer) IsValidToken(ctx context.Context, req *api.IsValidTokenRequest) (*api.IsValidTokenResponse, error) {
client := ctx.Value("kubeClient").(*v1.Client)
namespaces, err := client.ListOnepanelEnabledNamespaces()
if err != nil {
return nil, err
}
if len(namespaces) == 0 {
return nil, errors.New("No namespaces for onepanel setup.")
}
namespace := namespaces[0]
allowed, err := auth.IsAuthorized(client, "", "get", "", "namespaces", namespace.Name)
if err != nil {
return nil, err
}
if !allowed {
return &api.IsValidTokenResponse{
Valid: false,
}, nil
}
return &api.IsValidTokenResponse{
Valid: true,
}, nil
}