netmaker/netclient/auth/auth.go

99 lines
2.5 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"
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"
"io/ioutil"
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()
2021-08-31 03:58:23 +08:00
tokentext, err := ioutil.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
}
2021-08-31 03:58:23 +08:00
tokentext, err = ioutil.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,
Network: network,
}
data, err := json.Marshal(&node)
if err != nil {
return nil
}
login := &nodepb.Object{
Data: string(data),
}
// RPC call
res, err := client.Login(context.TODO(), login)
if err != nil {
return err
}
tokenstring := []byte(res.Data)
2021-08-31 03:58:23 +08:00
err = ioutil.WriteFile(home+"nettoken-"+network, tokenstring, 0644)
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)
2021-09-20 02:03:47 +08:00
err := ioutil.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) {
2021-09-20 02:03:47 +08:00
dat, err := ioutil.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
}