blob: 7aa4f1b29b327a8be861ff752a0dbcff5fb48cad [file] [log] [blame]
ychacon71789712023-05-15 11:07:32 +02001// -
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===================================
20package handler
21
22import (
23 "bytes"
24 "encoding/json"
25 "fmt"
26 "net/http"
27
28 "github.com/labstack/echo/v4"
29 log "github.com/sirupsen/logrus"
30 "oransc.org/nonrtric/capifinvoker/internal/securityapi"
31)
32
33func 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{}{
36 "isError": false,
37 "isResponse": false,
38 })
39}
40
41func ObtainSecurityMethod(server string) echo.HandlerFunc {
42 return func(c echo.Context) error {
43 log.Info("[Security API] in ObtainSecurityMethod")
44 invokerId := c.FormValue("invokerId")
45 if invokerId == "" {
46 log.Error("[Security API] field invokerId is needed")
47 return c.Render(http.StatusBadRequest, "securitymethod.html", map[string]interface{}{
48 "isError": true,
49 "isResponse": false,
50 "response": "field invokerId is needed",
51 })
52 }
53
54 //server format: http://localhost:8090
55 url := server + "/capif-security/v1/trustedInvokers/" + invokerId
56
57 log.Infof("[Security API] url to capif core %v for invokerId: %v", url, invokerId)
58 var servSecurity securityapi.ServiceSecurity
59
60 err := json.Unmarshal([]byte(c.FormValue("servSecurity")), &servSecurity)
61 if err != nil {
62 log.Error("[Security API] error unmarshaling parameter ServiceSecurity as JSON")
63 return c.Render(http.StatusBadRequest, "securitymethod.html", map[string]interface{}{
64 "isResponse": false,
65 "isError": true,
66 "response": "error unmarshaling parameter ServiceSecurity as JSON",
67 })
68 }
69
70 headers := map[string]string{
71 "Content-Type": "application/json",
72 }
73 jsonBytes, err := json.Marshal(servSecurity)
74 if err != nil {
75 return c.Render(http.StatusBadRequest, "securitymethod.html", map[string]interface{}{
76 "isResponse": false,
77 "isError": true,
78 "response": "Error marshaling parameter ServiceSecurity before doing request",
79 })
80 }
81 resp, err := makeRequest("PUT", url, headers, bytes.NewReader(jsonBytes))
82 if err != nil {
83 log.Errorf("[Security API] %v", fmt.Sprintf("error: %v", err))
84 return c.Render(http.StatusBadRequest, "securitymethod.html", map[string]interface{}{
85 "isResponse": false,
86 "isError": true,
87 "response": fmt.Sprintf("error: %v", err),
88 })
89 }
90
91 var resAPI securityapi.ServiceSecurity
92 err = json.Unmarshal(resp, &resAPI)
93 if err != nil {
94 log.Error("[Security API] error unmarshaling parameter ServiceSecurity as JSON")
95 return c.Render(http.StatusBadRequest, "securitymethod.html", map[string]interface{}{
96 "isResponse": false,
97 "isError": true,
98 "response": "Error unmarshaling parameter ServiceSecurity as JSON",
99 })
100 }
101
102 // Return the rendered response HTML
103 bytes, _ := json.Marshal(resAPI)
104 return c.Render(http.StatusOK, "securitymethod.html", map[string]interface{}{
105 "isResponse": true,
106 "response": string(bytes),
107 })
108 }
109}