Lift CAPIF specifications to latest version
[nonrtric/plt/sme.git] / capifcore / internal / securityservice / security.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2022: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package security
22
23 import (
24         "net/http"
25         "strings"
26
27         "github.com/labstack/echo/v4"
28
29         "oransc.org/nonrtric/capifcore/internal/common29122"
30         securityapi "oransc.org/nonrtric/capifcore/internal/securityapi"
31
32         "oransc.org/nonrtric/capifcore/internal/invokermanagement"
33         "oransc.org/nonrtric/capifcore/internal/providermanagement"
34         "oransc.org/nonrtric/capifcore/internal/publishservice"
35 )
36
37 type Security struct {
38         serviceRegister providermanagement.ServiceRegister
39         apiRegister     publishservice.APIRegister
40         invokerRegister invokermanagement.InvokerRegister
41 }
42
43 func NewSecurity(serviceRegister providermanagement.ServiceRegister, apiRegister publishservice.APIRegister, invokerRegister invokermanagement.InvokerRegister) *Security {
44         return &Security{
45                 serviceRegister: serviceRegister,
46                 apiRegister:     apiRegister,
47                 invokerRegister: invokerRegister,
48         }
49 }
50
51 func (s *Security) PostSecuritiesSecurityIdToken(ctx echo.Context, securityId string) error {
52         clientId := ctx.FormValue("client_id")
53         clientSecret := ctx.FormValue("client_secret")
54         scope := ctx.FormValue("scope")
55
56         if !s.invokerRegister.IsInvokerRegistered(clientId) {
57                 return sendCoreError(ctx, http.StatusBadRequest, "Invoker not registered")
58         }
59         if !s.invokerRegister.VerifyInvokerSecret(clientId, clientSecret) {
60                 return sendCoreError(ctx, http.StatusBadRequest, "Invoker secret not valid")
61         }
62         if scope != "" {
63                 scopeData := strings.Split(strings.Split(scope, "#")[1], ":")
64                 if !s.serviceRegister.IsFunctionRegistered(scopeData[0]) {
65                         return sendCoreError(ctx, http.StatusBadRequest, "Function not registered")
66                 }
67                 if !s.apiRegister.IsAPIRegistered(scopeData[0], scopeData[1]) {
68                         return sendCoreError(ctx, http.StatusBadRequest, "API not published")
69                 }
70         }
71
72         accessTokenResp := securityapi.AccessTokenRsp{
73                 AccessToken: "asdadfsrt dsr t5",
74                 ExpiresIn:   0,
75                 Scope:       &scope,
76                 TokenType:   "Bearer",
77         }
78
79         err := ctx.JSON(http.StatusCreated, accessTokenResp)
80         if err != nil {
81                 // Something really bad happened, tell Echo that our handler failed
82                 return err
83         }
84
85         return nil
86 }
87
88 func (s *Security) DeleteTrustedInvokersApiInvokerId(ctx echo.Context, apiInvokerId string) error {
89         return ctx.NoContent(http.StatusNotImplemented)
90 }
91
92 func (s *Security) GetTrustedInvokersApiInvokerId(ctx echo.Context, apiInvokerId string, params securityapi.GetTrustedInvokersApiInvokerIdParams) error {
93         return ctx.NoContent(http.StatusNotImplemented)
94 }
95
96 func (s *Security) PutTrustedInvokersApiInvokerId(ctx echo.Context, apiInvokerId string) error {
97         return ctx.NoContent(http.StatusNotImplemented)
98 }
99
100 func (s *Security) PostTrustedInvokersApiInvokerIdDelete(ctx echo.Context, apiInvokerId string) error {
101         return ctx.NoContent(http.StatusNotImplemented)
102 }
103
104 func (s *Security) PostTrustedInvokersApiInvokerIdUpdate(ctx echo.Context, apiInvokerId string) error {
105         return ctx.NoContent(http.StatusNotImplemented)
106 }
107
108 func sendCoreError(ctx echo.Context, code int, message string) error {
109         pd := common29122.ProblemDetails{
110                 Cause:  &message,
111                 Status: &code,
112         }
113         err := ctx.JSON(code, pd)
114         return err
115 }