Roll versions after J-Relase (master branch)
[nonrtric/plt/sme.git] / capifcore / internal / securityapi / typevalidation.go
index 90dbda3..4a9ee28 100644 (file)
@@ -21,6 +21,9 @@
 package securityapi
 
 import (
+       "errors"
+       "fmt"
+       "net/url"
        "strings"
 )
 
@@ -35,7 +38,7 @@ func (tokenReq AccessTokenReq) Validate() (bool, AccessTokenErr) {
        }
 
        //3gpp#aefId1:apiName1,apiName2,…apiNameX;aefId2:apiName1,apiName2,…apiNameY;…aefIdN:apiName1,apiName2,…apiNameZ
-       if tokenReq.Scope != nil {
+       if tokenReq.Scope != nil && *tokenReq.Scope != "" {
                scope := strings.Split(*tokenReq.Scope, "#")
                if len(scope) < 2 {
                        return false, createAccessTokenError(AccessTokenErrErrorInvalidScope, "Malformed scope")
@@ -54,6 +57,53 @@ func (tokenReq AccessTokenReq) Validate() (bool, AccessTokenErr) {
        return true, AccessTokenErr{}
 }
 
+func (ss ServiceSecurity) Validate() error {
+
+       if len(strings.TrimSpace(string(ss.NotificationDestination))) == 0 {
+               return errors.New("ServiceSecurity missing required notificationDestination")
+       }
+
+       if _, err := url.ParseRequestURI(string(ss.NotificationDestination)); err != nil {
+               return fmt.Errorf("ServiceSecurity has invalid notificationDestination, err=%s", err)
+       }
+
+       if len(ss.SecurityInfo) == 0 {
+               return errors.New("ServiceSecurity missing required SecurityInfo")
+       }
+       for _, securityInfo := range ss.SecurityInfo {
+               securityInfo.Validate()
+       }
+       return nil
+}
+
+func (si SecurityInformation) Validate() error {
+       if len(si.PrefSecurityMethods) == 0 {
+               return errors.New("SecurityInformation missing required PrefSecurityMethods")
+       }
+       return nil
+}
+
+func (sn SecurityNotification) Validate() error {
+
+       if len(strings.TrimSpace(string(sn.ApiInvokerId))) == 0 {
+               return errors.New("SecurityNotification missing required ApiInvokerId")
+       }
+
+       if len(sn.ApiIds) < 1 {
+               return errors.New("SecurityNotification missing required ApiIds")
+       }
+
+       if len(strings.TrimSpace(string(sn.Cause))) == 0 {
+               return errors.New("SecurityNotification missing required Cause")
+       }
+
+       if sn.Cause != CauseOVERLIMITUSAGE && sn.Cause != CauseUNEXPECTEDREASON {
+               return errors.New("SecurityNotification unexpected value for Cause")
+       }
+
+       return nil
+}
+
 func createAccessTokenError(err AccessTokenErrError, message string) AccessTokenErr {
        return AccessTokenErr{
                Error:            err,