auth.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package auth
  2. import (
  3. "os"
  4. "github.com/gravitl/netmaker/netclient/ncutils"
  5. // "os"
  6. )
  7. // SetJWT func will used to create the JWT while signing in and signing out
  8. //func SetJWT(client nodepb.NodeServiceClient, network string) (context.Context, error) {
  9. // home := ncutils.GetNetclientPathSpecific()
  10. // tokentext, err := os.ReadFile(home + "nettoken-" + network)
  11. // if err != nil {
  12. // err = AutoLogin(client, network)
  13. // if err != nil {
  14. // return nil, status.Errorf(codes.Unauthenticated, fmt.Sprintf("Something went wrong with Auto Login: %v", err))
  15. // }
  16. // tokentext, err = ncutils.GetFileWithRetry(home+"nettoken-"+network, 1)
  17. // if err != nil {
  18. // return nil, status.Errorf(codes.Unauthenticated, fmt.Sprintf("Something went wrong: %v", err))
  19. // }
  20. // }
  21. // token := string(tokentext)
  22. //
  23. // // Anything linked to this variable will transmit request headers.
  24. // md := metadata.New(map[string]string{"authorization": token})
  25. // ctx := context.Background()
  26. // ctx = metadata.NewOutgoingContext(ctx, md)
  27. // return ctx, nil
  28. //}
  29. // AutoLogin - auto logins whenever client needs to request from server
  30. //func AutoLogin(client nodepb.NodeServiceClient, network string) error {
  31. // home := ncutils.GetNetclientPathSpecific()
  32. // cfg, err := config.ReadConfig(network)
  33. // if err != nil {
  34. // return err
  35. // }
  36. // pass, err := RetrieveSecret(network)
  37. // if err != nil {
  38. // return err
  39. // }
  40. // node := models.Node{
  41. // Password: pass,
  42. // MacAddress: cfg.Node.MacAddress,
  43. // ID: cfg.Node.ID,
  44. // Network: network,
  45. // }
  46. // data, err := json.Marshal(&node)
  47. // if err != nil {
  48. // return nil
  49. // }
  50. //
  51. // login := &nodepb.Object{
  52. // Data: string(data),
  53. // Type: nodepb.NODE_TYPE,
  54. // }
  55. // // RPC call
  56. // res, err := client.Login(context.TODO(), login)
  57. // if err != nil {
  58. // return err
  59. // }
  60. // tokenstring := []byte(res.Data)
  61. // err = os.WriteFile(home+"nettoken-"+network, tokenstring, 0600)
  62. // if err != nil {
  63. // return err
  64. // }
  65. // return err
  66. //}
  67. // StoreSecret - stores auth secret locally
  68. func StoreSecret(key string, network string) error {
  69. d1 := []byte(key)
  70. return os.WriteFile(ncutils.GetNetclientPathSpecific()+"secret-"+network, d1, 0600)
  71. }
  72. // RetrieveSecret - fetches secret locally
  73. func RetrieveSecret(network string) (string, error) {
  74. dat, err := ncutils.GetFileWithRetry(ncutils.GetNetclientPathSpecific()+"secret-"+network, 3)
  75. return string(dat), err
  76. }
  77. // StoreTrafficKey - stores traffic key
  78. func StoreTrafficKey(key *[32]byte, network string) error {
  79. var data, err = ncutils.ConvertKeyToBytes(key)
  80. if err != nil {
  81. return err
  82. }
  83. return os.WriteFile(ncutils.GetNetclientPathSpecific()+"traffic-"+network, data, 0600)
  84. }
  85. // RetrieveTrafficKey - reads traffic file locally
  86. func RetrieveTrafficKey(network string) (*[32]byte, error) {
  87. data, err := ncutils.GetFileWithRetry(ncutils.GetNetclientPathSpecific()+"traffic-"+network, 2)
  88. if err != nil {
  89. return nil, err
  90. }
  91. return ncutils.ConvertBytesToKey(data)
  92. }
  93. // Configuraion - struct for mac and pass
  94. type Configuration struct {
  95. MacAddress string
  96. Password string
  97. }