netmaker/netclient/auth/auth.go

101 lines
2.6 KiB
Go
Raw Normal View History

2021-05-26 00:48:04 +08:00
package auth
2021-03-26 00:17:52 +08:00
import (
2021-08-03 06:06:26 +08:00
"encoding/json"
"fmt"
2022-01-07 04:05:38 +08:00
"os"
2021-03-26 00:17:52 +08:00
2021-08-03 06:06:26 +08:00
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/netclient/config"
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
2021-08-03 06:06:26 +08:00
// "os"
"context"
nodepb "github.com/gravitl/netmaker/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
2021-03-26 00:17:52 +08:00
)
2021-10-09 03:07:12 +08:00
// SetJWT func will used to create the JWT while signing in and signing out
func SetJWT(client nodepb.NodeServiceClient, network string) (context.Context, error) {
2021-09-20 02:03:47 +08:00
home := ncutils.GetNetclientPathSpecific()
2022-01-07 04:05:38 +08:00
tokentext, err := os.ReadFile(home + "nettoken-" + network)
2021-08-03 06:06:26 +08:00
if err != nil {
err = AutoLogin(client, network)
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, fmt.Sprintf("Something went wrong with Auto Login: %v", err))
2021-03-26 00:17:52 +08:00
}
2022-01-07 04:05:38 +08:00
tokentext, err = os.ReadFile(home + "nettoken-" + network)
2021-08-03 06:06:26 +08:00
if err != nil {
return nil, status.Errorf(codes.Unauthenticated, fmt.Sprintf("Something went wrong: %v", err))
}
}
token := string(tokentext)
2021-03-26 00:17:52 +08:00
2021-08-03 06:06:26 +08:00
// Anything linked to this variable will transmit request headers.
md := metadata.New(map[string]string{"authorization": token})
ctx := context.Background()
ctx = metadata.NewOutgoingContext(ctx, md)
return ctx, nil
2021-03-26 00:17:52 +08:00
}
2021-10-09 03:07:12 +08:00
// AutoLogin - auto logins whenever client needs to request from server
func AutoLogin(client nodepb.NodeServiceClient, network string) error {
2021-09-20 02:03:47 +08:00
home := ncutils.GetNetclientPathSpecific()
2021-08-03 06:06:26 +08:00
cfg, err := config.ReadConfig(network)
if err != nil {
return err
}
pass, err := RetrieveSecret(network)
if err != nil {
return err
}
node := models.Node{
Password: pass,
MacAddress: cfg.Node.MacAddress,
2022-01-11 23:58:20 +08:00
ID: cfg.Node.ID,
2021-08-03 06:06:26 +08:00
Network: network,
}
data, err := json.Marshal(&node)
if err != nil {
return nil
}
login := &nodepb.Object{
Data: string(data),
2022-01-11 23:58:20 +08:00
Type: nodepb.NODE_TYPE,
2021-08-03 06:06:26 +08:00
}
// RPC call
res, err := client.Login(context.TODO(), login)
if err != nil {
return err
}
tokenstring := []byte(res.Data)
2022-01-11 08:36:13 +08:00
err = os.WriteFile(home+"nettoken-"+network, tokenstring, 0644) // TODO: Proper permissions?
2021-08-03 06:06:26 +08:00
if err != nil {
return err
}
return err
}
2021-10-09 03:07:12 +08:00
// StoreSecret - stores auth secret locally
2021-08-03 06:06:26 +08:00
func StoreSecret(key string, network string) error {
d1 := []byte(key)
2022-01-07 04:05:38 +08:00
err := os.WriteFile(ncutils.GetNetclientPathSpecific()+"secret-"+network, d1, 0644)
2021-08-03 06:06:26 +08:00
return err
}
2021-10-09 03:07:12 +08:00
// RetrieveSecret - fetches secret locally
2021-08-03 06:06:26 +08:00
func RetrieveSecret(network string) (string, error) {
2022-01-07 04:05:38 +08:00
dat, err := os.ReadFile(ncutils.GetNetclientPathSpecific() + "secret-" + network)
2021-08-03 06:06:26 +08:00
return string(dat), err
2021-03-26 00:17:52 +08:00
}
2021-10-09 03:07:12 +08:00
// Configuraion - struct for mac and pass
2021-03-26 00:17:52 +08:00
type Configuration struct {
MacAddress string
2021-08-03 06:06:26 +08:00
Password string
2021-03-26 00:17:52 +08:00
}