Seed code
[nonrtric/plt/sme.git] / internal / security / 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/sme/internal/common29122"
30         securityapi "oransc.org/nonrtric/sme/internal/securityapi"
31
32         "oransc.org/nonrtric/sme/internal/invokermanagement"
33         "oransc.org/nonrtric/sme/internal/providermanagement"
34         "oransc.org/nonrtric/sme/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         // grantType := ctx.FormValue("grant_type")
55         scope := ctx.FormValue("scope")
56
57         if !s.invokerRegister.IsInvokerRegistered(clientId) {
58                 return sendCoreError(ctx, http.StatusBadRequest, "Invoker not registered")
59         }
60         if !s.invokerRegister.VerifyInvokerSecret(clientId, clientSecret) {
61                 return sendCoreError(ctx, http.StatusBadRequest, "Invoker secret not valid")
62         }
63         if scope != "" {
64                 scopeData := strings.Split(strings.Split(scope, "#")[1], ":")
65                 if !s.serviceRegister.IsFunctionRegistered(scopeData[0]) {
66                         return sendCoreError(ctx, http.StatusBadRequest, "Function not registered")
67                 }
68                 if !s.apiRegister.IsAPIRegistered(scopeData[0], scopeData[1]) {
69                         return sendCoreError(ctx, http.StatusBadRequest, "API not published")
70                 }
71         }
72
73         accessTokenResp := securityapi.AccessTokenRsp{
74                 AccessToken: "asdadfsrt dsr t5",
75                 ExpiresIn:   0,
76                 Scope:       &scope,
77                 TokenType:   "Bearer",
78         }
79
80         err := ctx.JSON(http.StatusCreated, accessTokenResp)
81         if err != nil {
82                 // Something really bad happened, tell Echo that our handler failed
83                 return err
84         }
85
86         return nil
87 }
88
89 func (s *Security) DeleteTrustedInvokersApiInvokerId(ctx echo.Context, apiInvokerId string) error {
90         return ctx.NoContent(http.StatusNotImplemented)
91 }
92
93 func (s *Security) GetTrustedInvokersApiInvokerId(ctx echo.Context, apiInvokerId string, params securityapi.GetTrustedInvokersApiInvokerIdParams) error {
94         return ctx.NoContent(http.StatusNotImplemented)
95 }
96
97 func (s *Security) PutTrustedInvokersApiInvokerId(ctx echo.Context, apiInvokerId string) error {
98         return ctx.NoContent(http.StatusNotImplemented)
99 }
100
101 func (s *Security) PostTrustedInvokersApiInvokerIdDelete(ctx echo.Context, apiInvokerId string) error {
102         return ctx.NoContent(http.StatusNotImplemented)
103 }
104
105 func (s *Security) PostTrustedInvokersApiInvokerIdUpdate(ctx echo.Context, apiInvokerId string) error {
106         return ctx.NoContent(http.StatusNotImplemented)
107 }
108
109 func sendCoreError(ctx echo.Context, code int, message string) error {
110         pd := common29122.ProblemDetails{
111                 Cause:  &message,
112                 Status: &code,
113         }
114         err := ctx.JSON(code, pd)
115         return err
116 }