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===================================
28 "github.com/labstack/echo/v4"
29 log "github.com/sirupsen/logrus"
30 "oransc.org/nonrtric/capifinvoker/internal/securityapi"
33 func SecurityMethodHandler(c echo.Context) error {
34 log.Info("[Security API] in security method handler")
35 return c.Render(http.StatusOK, "securitymethod.html", map[string]interface{}{
41 func ObtainSecurityMethod(server string) echo.HandlerFunc {
42 return func(c echo.Context) error {
43 log.Info("[Security API] in ObtainSecurityMethod")
44 invokerId := c.FormValue("invokerId")
46 log.Error("[Security API] field invokerId is needed")
47 return c.Render(http.StatusBadRequest, "securitymethod.html", map[string]interface{}{
50 "response": "field invokerId is needed",
54 //server format: http://localhost:8090
55 url := server + "/capif-security/v1/trustedInvokers/" + invokerId
57 log.Infof("[Security API] url to capif core %v for invokerId: %v", url, invokerId)
58 var servSecurity securityapi.ServiceSecurity
60 err := json.Unmarshal([]byte(c.FormValue("servSecurity")), &servSecurity)
62 log.Error("[Security API] error unmarshaling parameter ServiceSecurity as JSON")
63 return c.Render(http.StatusBadRequest, "securitymethod.html", map[string]interface{}{
66 "response": "error unmarshaling parameter ServiceSecurity as JSON",
70 headers := map[string]string{
71 "Content-Type": "application/json",
73 jsonBytes, err := json.Marshal(servSecurity)
75 return c.Render(http.StatusBadRequest, "securitymethod.html", map[string]interface{}{
78 "response": "Error marshaling parameter ServiceSecurity before doing request",
81 resp, err := makeRequest("PUT", url, headers, bytes.NewReader(jsonBytes))
83 log.Errorf("[Security API] %v", fmt.Sprintf("error: %v", err))
84 return c.Render(http.StatusBadRequest, "securitymethod.html", map[string]interface{}{
87 "response": fmt.Sprintf("error: %v", err),
91 var resAPI securityapi.ServiceSecurity
92 err = json.Unmarshal(resp, &resAPI)
94 log.Error("[Security API] error unmarshaling parameter ServiceSecurity as JSON")
95 return c.Render(http.StatusBadRequest, "securitymethod.html", map[string]interface{}{
98 "response": "Error unmarshaling parameter ServiceSecurity as JSON",
102 // Return the rendered response HTML
103 bytes, _ := json.Marshal(resAPI)
104 return c.Render(http.StatusOK, "securitymethod.html", map[string]interface{}{
106 "response": string(bytes),