NONRTRIC-946: Servicemanager - mock kong and capif as library
[nonrtric/plt/sme.git] / capifcore / internal / securityservice / security.go
index e211f67..b3f1d30 100644 (file)
@@ -2,7 +2,8 @@
 //   ========================LICENSE_START=================================
 //   O-RAN-SC
 //   %%
-//   Copyright (C) 2022: Nordix Foundation
+//   Copyright (C) 2022-2023: Nordix Foundation
+//   Copyright (C) 2024: OpenInfra Foundation Europe
 //   %%
 //   Licensed under the Apache License, Version 2.0 (the "License");
 //   you may not use this file except in compliance with the License.
@@ -91,9 +92,17 @@ func (s *Security) PostSecuritiesSecurityIdToken(ctx echo.Context, securityId st
                }
        }
        data := url.Values{"grant_type": {"client_credentials"}, "client_id": {accessTokenReq.ClientId}, "client_secret": {*accessTokenReq.ClientSecret}}
-       jwtToken, err := s.keycloak.GetToken("invokerrealm", data)
-       if err != nil {
-               return sendAccessTokenError(ctx, http.StatusBadRequest, securityapi.AccessTokenErrErrorUnauthorizedClient, err.Error())
+
+       var jwtToken keycloak.Jwttoken
+       var err error
+
+       if s.keycloak != nil {
+               jwtToken, err = s.keycloak.GetToken("invokerrealm", data)
+               if err != nil {
+                       return sendAccessTokenError(ctx, http.StatusBadRequest, securityapi.AccessTokenErrErrorUnauthorizedClient, err.Error())
+               }
+       } else {
+               return sendAccessTokenError(ctx, http.StatusBadRequest, securityapi.AccessTokenErrErrorUnauthorizedClient, "keycloak is nil")
        }
 
        accessTokenResp := securityapi.AccessTokenRsp{
@@ -192,11 +201,6 @@ func (s *Security) PutTrustedInvokersApiInvokerId(ctx echo.Context, apiInvokerId
                return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
        }
 
-       err = s.keycloak.AddClient(apiInvokerId, "invokerrealm")
-       if err != nil {
-               return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
-       }
-
        uri := ctx.Request().Host + ctx.Request().URL.String()
        ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, apiInvokerId))
 
@@ -245,7 +249,7 @@ func (s *Security) PostTrustedInvokersApiInvokerIdDelete(ctx echo.Context, apiIn
        }
 
        if ss, ok := s.trustedInvokers[apiInvokerId]; ok {
-               securityInfoCopy := s.revokeTrustedInvoker(&ss, notification, apiInvokerId)
+               securityInfoCopy := s.revokeTrustedInvoker(&ss, notification)
 
                if len(securityInfoCopy) == 0 {
                        s.deleteTrustedInvoker(apiInvokerId)
@@ -262,7 +266,7 @@ func (s *Security) PostTrustedInvokersApiInvokerIdDelete(ctx echo.Context, apiIn
 
 }
 
-func (s *Security) revokeTrustedInvoker(ss *securityapi.ServiceSecurity, notification securityapi.SecurityNotification, apiInvokerId string) []securityapi.SecurityInformation {
+func (s *Security) revokeTrustedInvoker(ss *securityapi.ServiceSecurity, notification securityapi.SecurityNotification) []securityapi.SecurityInformation {
 
        data, _ := copystructure.Copy(ss.SecurityInfo)
        securityInfoCopy, _ := data.([]securityapi.SecurityInformation)