http_client.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. package functions
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "log"
  8. "net/http"
  9. "net/url"
  10. "os"
  11. "os/signal"
  12. "strings"
  13. "time"
  14. "github.com/gorilla/websocket"
  15. "github.com/gravitl/netmaker/cli/config"
  16. "github.com/gravitl/netmaker/logger"
  17. "github.com/gravitl/netmaker/models"
  18. "golang.org/x/exp/slog"
  19. )
  20. const (
  21. ambBaseUrl = "https://api.accounts.netmaker.io"
  22. TenantUrlTemplate = "https://api-%s.app.prod.netmaker.io"
  23. ambOauthWssUrl = "wss://api.accounts.netmaker.io/api/v1/auth/sso"
  24. )
  25. func ssoLogin(endpoint string) string {
  26. var (
  27. authToken string
  28. interrupt = make(chan os.Signal, 1)
  29. url, _ = url.Parse(endpoint)
  30. socketURL = fmt.Sprintf("wss://%s/api/oauth/headless", url.Host)
  31. )
  32. signal.Notify(interrupt, os.Interrupt)
  33. conn, _, err := websocket.DefaultDialer.Dial(socketURL, nil)
  34. if err != nil {
  35. log.Fatal("error connecting to endpoint ", socketURL, err.Error())
  36. }
  37. defer conn.Close()
  38. _, msg, err := conn.ReadMessage()
  39. if err != nil {
  40. log.Fatal("error reading from server: ", err.Error())
  41. }
  42. fmt.Printf("Please visit:\n %s \n to authenticate\n", string(msg))
  43. done := make(chan struct{})
  44. defer close(done)
  45. go func() {
  46. for {
  47. msgType, msg, err := conn.ReadMessage()
  48. if err != nil {
  49. if msgType < 0 {
  50. done <- struct{}{}
  51. return
  52. }
  53. if !strings.Contains(err.Error(), "normal") {
  54. log.Fatal("read error: ", err.Error())
  55. }
  56. return
  57. }
  58. if msgType == websocket.CloseMessage {
  59. done <- struct{}{}
  60. return
  61. }
  62. if strings.Contains(string(msg), "JWT: ") {
  63. authToken = strings.TrimPrefix(string(msg), "JWT: ")
  64. } else {
  65. logger.Log(0, "Message from server:", string(msg))
  66. return
  67. }
  68. }
  69. }()
  70. for {
  71. select {
  72. case <-done:
  73. return authToken
  74. case <-interrupt:
  75. err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
  76. if err != nil {
  77. logger.Log(0, "write close:", err.Error())
  78. }
  79. return authToken
  80. }
  81. }
  82. }
  83. func getAuthToken(ctx config.Context, force bool) string {
  84. if !force && ctx.AuthToken != "" {
  85. return ctx.AuthToken
  86. }
  87. if !ctx.Saas {
  88. if ctx.SSO {
  89. authToken := ssoLogin(ctx.Endpoint)
  90. config.SetAuthToken(authToken)
  91. return authToken
  92. }
  93. authParams := &models.UserAuthParams{UserName: ctx.Username, Password: ctx.Password}
  94. payload, err := json.Marshal(authParams)
  95. if err != nil {
  96. log.Fatal(err)
  97. }
  98. res, err := http.Post(ctx.Endpoint+"/api/users/adm/authenticate", "application/json", bytes.NewReader(payload))
  99. if err != nil {
  100. log.Fatal(err)
  101. }
  102. defer res.Body.Close()
  103. resBodyBytes, err := io.ReadAll(res.Body)
  104. if err != nil {
  105. log.Fatalf("Client could not read response body: %s", err)
  106. }
  107. if res.StatusCode != http.StatusOK {
  108. log.Fatalf("Error Status: %d Response: %s", res.StatusCode, string(resBodyBytes))
  109. }
  110. body := new(models.SuccessResponse)
  111. if err := json.Unmarshal(resBodyBytes, body); err != nil {
  112. log.Fatalf("Error unmarshalling JSON: %s", err)
  113. }
  114. authToken := body.Response.(map[string]any)["AuthToken"].(string)
  115. config.SetAuthToken(authToken)
  116. return authToken
  117. }
  118. if !ctx.SSO {
  119. sToken, _, err := basicAuthSaasSignin(ctx.Username, ctx.Password)
  120. if err != nil {
  121. log.Fatal(err)
  122. }
  123. authToken, _, err := tenantLogin(ctx, sToken)
  124. if err != nil {
  125. log.Fatal(err)
  126. }
  127. config.SetAuthToken(authToken)
  128. return authToken
  129. }
  130. accessToken, err := loginSaaSOauth(&models.SsoLoginReqDto{OauthProvider: "oidc"}, ctx.TenantId)
  131. if err != nil {
  132. log.Fatal(err)
  133. }
  134. config.SetAuthToken(accessToken)
  135. return accessToken
  136. }
  137. func request[T any](method, route string, payload any) *T {
  138. var (
  139. _, ctx = config.GetCurrentContext()
  140. req *http.Request
  141. err error
  142. )
  143. if payload == nil {
  144. req, err = http.NewRequest(method, ctx.Endpoint+route, nil)
  145. if err != nil {
  146. log.Fatalf("Client could not create request: %s", err)
  147. }
  148. } else {
  149. payloadBytes, jsonErr := json.Marshal(payload)
  150. if jsonErr != nil {
  151. log.Fatalf("Error in request JSON marshalling: %s", err)
  152. }
  153. req, err = http.NewRequest(method, ctx.Endpoint+route, bytes.NewReader(payloadBytes))
  154. if err != nil {
  155. log.Fatalf("Client could not create request: %s", err)
  156. }
  157. req.Header.Set("Content-Type", "application/json")
  158. }
  159. if ctx.MasterKey != "" {
  160. req.Header.Set("Authorization", "Bearer "+ctx.MasterKey)
  161. } else {
  162. req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, false))
  163. }
  164. retried := false
  165. retry:
  166. res, err := http.DefaultClient.Do(req)
  167. if err != nil {
  168. log.Fatalf("Client error making http request: %s", err)
  169. }
  170. // refresh JWT token
  171. if res.StatusCode == http.StatusUnauthorized && !retried && ctx.MasterKey == "" {
  172. req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, true))
  173. retried = true
  174. // TODO add a retry limit, drop goto
  175. goto retry
  176. }
  177. resBodyBytes, err := io.ReadAll(res.Body)
  178. if err != nil {
  179. log.Fatalf("Client could not read response body: %s", err)
  180. }
  181. if res.StatusCode != http.StatusOK {
  182. log.Fatalf("Error Status: %d Response: %s", res.StatusCode, string(resBodyBytes))
  183. }
  184. body := new(T)
  185. if len(resBodyBytes) > 0 {
  186. if err := json.Unmarshal(resBodyBytes, body); err != nil {
  187. log.Fatalf("Error unmarshalling JSON: %s", err)
  188. }
  189. }
  190. return body
  191. }
  192. func get(route string) string {
  193. _, ctx := config.GetCurrentContext()
  194. req, err := http.NewRequest(http.MethodGet, ctx.Endpoint+route, nil)
  195. if err != nil {
  196. log.Fatal(err)
  197. }
  198. if ctx.MasterKey != "" {
  199. req.Header.Set("Authorization", "Bearer "+ctx.MasterKey)
  200. } else {
  201. req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, true))
  202. }
  203. res, err := http.DefaultClient.Do(req)
  204. if err != nil {
  205. log.Fatal(err)
  206. }
  207. bodyBytes, err := io.ReadAll(res.Body)
  208. if err != nil {
  209. log.Fatal(err)
  210. }
  211. return string(bodyBytes)
  212. }
  213. func basicAuthSaasSignin(email, password string) (string, http.Header, error) {
  214. payload := models.SignInReqDto{
  215. FormFields: []models.FormField{
  216. {
  217. Id: "email",
  218. Value: email,
  219. },
  220. {
  221. Id: "password",
  222. Value: password,
  223. },
  224. },
  225. }
  226. var res models.SignInResDto
  227. // Create a new HTTP client with a timeout
  228. client := &http.Client{
  229. Timeout: 30 * time.Second,
  230. }
  231. // Create the request body
  232. payloadBuf := new(bytes.Buffer)
  233. json.NewEncoder(payloadBuf).Encode(payload)
  234. // Create the request
  235. req, err := http.NewRequest("POST", ambBaseUrl+"/auth/signin", payloadBuf)
  236. if err != nil {
  237. return "", http.Header{}, err
  238. }
  239. req.Header.Set("Content-Type", "application/json; charset=utf-8")
  240. req.Header.Set("rid", "thirdpartyemailpassword")
  241. // Send the request
  242. resp, err := client.Do(req)
  243. if err != nil {
  244. return "", http.Header{}, err
  245. }
  246. defer resp.Body.Close()
  247. // Check the response status code
  248. if resp.StatusCode != http.StatusOK {
  249. return "", http.Header{}, fmt.Errorf("error authenticating: %s", resp.Status)
  250. }
  251. // Copy the response headers
  252. resHeaders := resp.Header
  253. // Decode the response body
  254. err = json.NewDecoder(resp.Body).Decode(&res)
  255. if err != nil {
  256. return "", http.Header{}, err
  257. }
  258. sToken := resHeaders.Get(models.ResHeaderKeyStAccessToken)
  259. encodedAccessToken := url.QueryEscape(sToken)
  260. return encodedAccessToken, resHeaders, nil
  261. }
  262. func tenantLogin(ctx config.Context, sToken string) (string, string, error) {
  263. url := fmt.Sprintf("%s/api/v1/tenant/login?tenant_id=%s", ambBaseUrl, ctx.TenantId)
  264. client := &http.Client{}
  265. req, err := http.NewRequest(http.MethodPost, url, nil)
  266. if err != nil {
  267. return "", "", err
  268. }
  269. req.Header.Add("Cookie", fmt.Sprintf("sAccessToken=%s", sToken))
  270. res, err := client.Do(req)
  271. if err != nil {
  272. return "", "", err
  273. }
  274. defer res.Body.Close()
  275. body, err := io.ReadAll(res.Body)
  276. if err != nil {
  277. return "", "", err
  278. }
  279. data := models.TenantLoginResDto{}
  280. json.Unmarshal(body, &data)
  281. return data.Response.AuthToken, fmt.Sprintf(TenantUrlTemplate, ctx.TenantId), nil
  282. }
  283. func loginSaaSOauth(payload *models.SsoLoginReqDto, tenantId string) (string, error) {
  284. socketUrl := ambOauthWssUrl
  285. // Dial the netmaker server controller
  286. conn, _, err := websocket.DefaultDialer.Dial(socketUrl, nil)
  287. if err != nil {
  288. slog.Error("error connecting to endpoint ", "url", socketUrl, "err", err)
  289. return "", err
  290. }
  291. defer conn.Close()
  292. return handleServerSSORegisterConn(payload, conn, tenantId)
  293. }
  294. func handleServerSSORegisterConn(payload *models.SsoLoginReqDto, conn *websocket.Conn, tenantId string) (string, error) {
  295. reqData, err := json.Marshal(payload)
  296. if err != nil {
  297. return "", err
  298. }
  299. if err := conn.WriteMessage(websocket.TextMessage, reqData); err != nil {
  300. return "", err
  301. }
  302. dataCh := make(chan string)
  303. defer close(dataCh)
  304. interrupt := make(chan os.Signal, 1)
  305. signal.Notify(interrupt, os.Interrupt)
  306. go func() {
  307. for {
  308. msgType, msg, err := conn.ReadMessage()
  309. if err != nil {
  310. if msgType < 0 {
  311. slog.Info("received close message from server")
  312. return
  313. }
  314. if !strings.Contains(err.Error(), "normal") { // Error reading a message from the server
  315. slog.Error("error msg", "err", err)
  316. }
  317. return
  318. }
  319. if msgType == websocket.CloseMessage {
  320. slog.Info("received close message from server")
  321. return
  322. }
  323. if strings.Contains(string(msg), "auth/sso") {
  324. fmt.Printf("Please visit:\n %s \nto authenticate\n", string(msg))
  325. } else {
  326. var res models.SsoLoginData
  327. if err := json.Unmarshal(msg, &res); err != nil {
  328. return
  329. }
  330. accessToken, _, err := tenantLoginV2(res.AmbAccessToken, tenantId, res.Username)
  331. if err != nil {
  332. slog.Error("error logging in tenant", "err", err)
  333. dataCh <- ""
  334. return
  335. }
  336. dataCh <- accessToken
  337. return
  338. }
  339. }
  340. }()
  341. for {
  342. select {
  343. case accessToken := <-dataCh:
  344. if accessToken == "" {
  345. slog.Info("error getting access token")
  346. return "", fmt.Errorf("error getting access token")
  347. }
  348. return accessToken, nil
  349. case <-time.After(30 * time.Second):
  350. slog.Error("authentiation timed out")
  351. os.Exit(1)
  352. case <-interrupt:
  353. slog.Info("interrupt received, closing connection")
  354. // Cleanly close the connection by sending a close message and then
  355. // waiting (with timeout) for the server to close the connection.
  356. err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
  357. if err != nil {
  358. log.Fatal(err)
  359. }
  360. os.Exit(1)
  361. }
  362. }
  363. }
  364. func tenantLoginV2(ambJwt, tenantId, email string) (string, string, error) {
  365. url := fmt.Sprintf("%s/api/v1/tenant/login/custom", ambBaseUrl)
  366. payload := models.LoginReqDto{
  367. Email: email,
  368. TenantID: tenantId,
  369. }
  370. payloadBuf := new(bytes.Buffer)
  371. json.NewEncoder(payloadBuf).Encode(payload)
  372. client := &http.Client{}
  373. req, err := http.NewRequest("POST", url, payloadBuf)
  374. if err != nil {
  375. slog.Error("error creating request", "err", err)
  376. return "", "", err
  377. }
  378. req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", ambJwt))
  379. res, err := client.Do(req)
  380. if err != nil {
  381. slog.Error("error sending request", "err", err)
  382. return "", "", err
  383. }
  384. defer res.Body.Close()
  385. body, err := io.ReadAll(res.Body)
  386. if err != nil {
  387. slog.Error("error reading response body", "err", err)
  388. return "", "", err
  389. }
  390. data := models.TenantLoginResDto{}
  391. json.Unmarshal(body, &data)
  392. return data.Response.AuthToken, fmt.Sprintf(TenantUrlTemplate, tenantId), nil
  393. }