3 // ========================LICENSE_START=================================
6 // Copyright (C) 2023: Nordix Foundation
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
12 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
30 "github.com/labstack/echo/v4"
31 log "github.com/sirupsen/logrus"
32 "oransc.org/nonrtric/capifinvoker/internal/securityapi"
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{}{
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")
48 log.Error("[Security API] field securityId is needed")
49 return c.Render(http.StatusBadRequest, "gettoken.html", map[string]interface{}{
52 "response": "field securityId is needed",
56 //server format: http://localhost:8090
57 urlStr := server + "/capif-security/v1/securities/" + securityId + "/token"
59 log.Infof("[Security API] url to capif core %v for securityId: %v", urlStr, securityId)
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"))
67 headers := map[string]string{
68 "Content-Type": "application/x-www-form-urlencoded",
69 "Content-Length": strconv.Itoa(len(data.Encode())),
71 resp, err := makeRequest("POST", urlStr, headers, strings.NewReader(data.Encode()))
73 log.Errorf("[Security API] %v", fmt.Sprintf("error: %v", err))
74 return c.Render(http.StatusBadRequest, "gettoken.html", map[string]interface{}{
77 "response": fmt.Sprintf("error: %v", err),
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{}{
87 "response": "Error unmarshaling parameter AccessTokenRsp as JSON",
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{}{
97 "response": string(bytes),