2 // ========================LICENSE_START=================================
5 // Copyright (C) 2023: Nordix Foundation
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
30 func (tokenReq AccessTokenReq) Validate() (bool, AccessTokenErr) {
32 if tokenReq.ClientId == "" {
33 return false, createAccessTokenError(AccessTokenErrErrorInvalidRequest, "Invalid request")
36 if tokenReq.GrantType != AccessTokenReqGrantTypeClientCredentials {
37 return false, createAccessTokenError(AccessTokenErrErrorInvalidGrant, "Invalid value for grant_type")
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, "#")
44 return false, createAccessTokenError(AccessTokenErrErrorInvalidScope, "Malformed scope")
46 if scope[0] != "3gpp" {
47 return false, createAccessTokenError(AccessTokenErrErrorInvalidScope, "Scope should start with 3gpp")
49 aefList := strings.Split(scope[1], ";")
50 for _, aef := range aefList {
51 apiList := strings.Split(aef, ":")
53 return false, createAccessTokenError(AccessTokenErrErrorInvalidScope, "Malformed scope")
57 return true, AccessTokenErr{}
60 func (ss ServiceSecurity) Validate() error {
62 if len(strings.TrimSpace(string(ss.NotificationDestination))) == 0 {
63 return errors.New("ServiceSecurity missing required notificationDestination")
66 if _, err := url.ParseRequestURI(string(ss.NotificationDestination)); err != nil {
67 return fmt.Errorf("ServiceSecurity has invalid notificationDestination, err=%s", err)
70 if len(ss.SecurityInfo) == 0 {
71 return errors.New("ServiceSecurity missing required SecurityInfo")
73 for _, securityInfo := range ss.SecurityInfo {
74 securityInfo.Validate()
79 func (si SecurityInformation) Validate() error {
80 if len(si.PrefSecurityMethods) == 0 {
81 return errors.New("SecurityInformation missing required PrefSecurityMethods")
86 func (sn SecurityNotification) Validate() error {
88 if len(strings.TrimSpace(string(sn.ApiInvokerId))) == 0 {
89 return errors.New("SecurityNotification missing required ApiInvokerId")
92 if len(sn.ApiIds) < 1 {
93 return errors.New("SecurityNotification missing required ApiIds")
96 if len(strings.TrimSpace(string(sn.Cause))) == 0 {
97 return errors.New("SecurityNotification missing required Cause")
100 if sn.Cause != CauseOVERLIMITUSAGE && sn.Cause != CauseUNEXPECTEDREASON {
101 return errors.New("SecurityNotification unexpected value for Cause")
107 func createAccessTokenError(err AccessTokenErrError, message string) AccessTokenErr {
108 return AccessTokenErr{
110 ErrorDescription: &message,