NONRTRIC-946: Servicemanager - Add Read The Docs
[nonrtric/plt/sme.git] / invoker / handler / gettoken_handler.go
1 // -
2 //
3 //      ========================LICENSE_START=================================
4 //      O-RAN-SC
5 //      %%
6 //      Copyright (C) 2023: Nordix Foundation
7 //      %%
8 //      Licensed under the Apache License, Version 2.0 (the "License");
9 //      you may not use this file except in compliance with the License.
10 //      You may obtain a copy of the License at
11 //
12 //           http://www.apache.org/licenses/LICENSE-2.0
13 //
14 //      Unless required by applicable law or agreed to in writing, software
15 //      distributed under the License is distributed on an "AS IS" BASIS,
16 //      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 //      See the License for the specific language governing permissions and
18 //      limitations under the License.
19 //      ========================LICENSE_END===================================
20 package handler
21
22 import (
23         "encoding/json"
24         "fmt"
25         "net/http"
26         "net/url"
27         "strconv"
28         "strings"
29
30         "github.com/labstack/echo/v4"
31         log "github.com/sirupsen/logrus"
32         "oransc.org/nonrtric/capifinvoker/internal/securityapi"
33 )
34
35 func GetTokenHandler(c echo.Context) error {
36         log.Info("[Security API] in get token handler")
37         return c.Render(http.StatusOK, "gettoken.html", map[string]interface{}{
38                 "isError":    false,
39                 "isResponse": false,
40         })
41 }
42
43 func ObtainToken(server string) echo.HandlerFunc {
44         return func(c echo.Context) error {
45                 log.Info("[Security API] in ObtainToken")
46                 securityId := c.FormValue("securityId")
47                 if securityId == "" {
48                         log.Error("[Security API] field securityId is needed")
49                         return c.Render(http.StatusBadRequest, "gettoken.html", map[string]interface{}{
50                                 "isError":    true,
51                                 "isResponse": false,
52                                 "response":   "field securityId is needed",
53                         })
54                 }
55
56                 //server format: http://localhost:8090
57                 urlStr := server + "/capif-security/v1/securities/" + securityId + "/token"
58
59                 log.Infof("[Security API] url to capif core %v for securityId: %v", urlStr, securityId)
60
61                 data := url.Values{}
62                 data.Set("client_id", c.FormValue("clientId"))
63                 data.Set("client_secret", c.FormValue("clientSecret"))
64                 data.Set("grant_type", "client_credentials")
65                 data.Set("scope", c.FormValue("scope"))
66
67                 headers := map[string]string{
68                         "Content-Type":   "application/x-www-form-urlencoded",
69                         "Content-Length": strconv.Itoa(len(data.Encode())),
70                 }
71                 resp, err := makeRequest("POST", urlStr, headers, strings.NewReader(data.Encode()))
72                 if err != nil {
73                         log.Errorf("[Security API] %v", fmt.Sprintf("error: %v", err))
74                         return c.Render(http.StatusBadRequest, "gettoken.html", map[string]interface{}{
75                                 "isResponse": false,
76                                 "isError":    true,
77                                 "response":   fmt.Sprintf("error: %v", err),
78                         })
79                 }
80
81                 var resToken securityapi.AccessTokenRsp
82                 if err = json.Unmarshal(resp, &resToken); err != nil {
83                         log.Error("[Security API] error unmarshaling parameter AccessTokenRsp as JSON")
84                         return c.Render(http.StatusBadRequest, "gettoken.html", map[string]interface{}{
85                                 "isResponse": false,
86                                 "isError":    true,
87                                 "response":   "Error unmarshaling parameter AccessTokenRsp as JSON",
88                         })
89                 }
90
91                 // Return the rendered response HTML
92                 bytes, _ := json.Marshal(resToken)
93                 log.Infof("[Security API] jwt token fetch AccessTokenRsp is %v\n", resToken)
94                 return c.Render(http.StatusOK, "gettoken.html", map[string]interface{}{
95                         "isResponse": true,
96                         "isError":    false,
97                         "response":   string(bytes),
98                 })
99         }
100 }