Implementation for PUT trustedInvokers endpoint
[nonrtric/plt/sme.git] / capifcore / internal / securityapi / typevalidation.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2023: 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 securityapi
22
23 import (
24         "errors"
25         "fmt"
26         "net/url"
27         "strings"
28 )
29
30 func (tokenReq AccessTokenReq) Validate() (bool, AccessTokenErr) {
31
32         if tokenReq.ClientId == "" {
33                 return false, createAccessTokenError(AccessTokenErrErrorInvalidRequest, "Invalid request")
34         }
35
36         if tokenReq.GrantType != AccessTokenReqGrantTypeClientCredentials {
37                 return false, createAccessTokenError(AccessTokenErrErrorInvalidGrant, "Invalid value for grant_type")
38         }
39
40         //3gpp#aefId1:apiName1,apiName2,…apiNameX;aefId2:apiName1,apiName2,…apiNameY;…aefIdN:apiName1,apiName2,…apiNameZ
41         if tokenReq.Scope != nil && *tokenReq.Scope != "" {
42                 scope := strings.Split(*tokenReq.Scope, "#")
43                 if len(scope) < 2 {
44                         return false, createAccessTokenError(AccessTokenErrErrorInvalidScope, "Malformed scope")
45                 }
46                 if scope[0] != "3gpp" {
47                         return false, createAccessTokenError(AccessTokenErrErrorInvalidScope, "Scope should start with 3gpp")
48                 }
49                 aefList := strings.Split(scope[1], ";")
50                 for _, aef := range aefList {
51                         apiList := strings.Split(aef, ":")
52                         if len(apiList) < 2 {
53                                 return false, createAccessTokenError(AccessTokenErrErrorInvalidScope, "Malformed scope")
54                         }
55                 }
56         }
57         return true, AccessTokenErr{}
58 }
59
60 func (ss ServiceSecurity) Validate() error {
61
62         if len(strings.TrimSpace(string(ss.NotificationDestination))) == 0 {
63                 return errors.New("ServiceSecurity missing required notificationDestination")
64         }
65
66         if _, err := url.ParseRequestURI(string(ss.NotificationDestination)); err != nil {
67                 return fmt.Errorf("ServiceSecurity has invalid notificationDestination, err=%s", err)
68         }
69
70         if len(ss.SecurityInfo) == 0 {
71                 return errors.New("ServiceSecurity missing required SecurityInfo")
72         }
73         for _, securityInfo := range ss.SecurityInfo {
74                 securityInfo.Validate()
75         }
76         return nil
77 }
78
79 func (si SecurityInformation) Validate() error {
80         if len(si.PrefSecurityMethods) == 0 {
81                 return errors.New("SecurityInformation missing required PrefSecurityMethods")
82         }
83         return nil
84 }
85
86 func createAccessTokenError(err AccessTokenErrError, message string) AccessTokenErr {
87         return AccessTokenErr{
88                 Error:            err,
89                 ErrorDescription: &message,
90         }
91 }