From bd8949eb8c1623e0f2ee12c53d01ac559f2d859e Mon Sep 17 00:00:00 2001 From: Trial97 Date: Mon, 27 Apr 2020 17:06:16 +0300 Subject: [PATCH] Added Stir/Shaken APIs to DispatcherS --- apier/v1/api_interfaces.go | 3 ++ apier/v1/dispatcher.go | 7 +++ data/tariffplans/dispatchers/Attributes.csv | 2 +- .../dispatchers_gob/Attributes.csv | 2 +- dispatchers/sessions.go | 38 +++++++++++++++ dispatchers/sessions_it_test.go | 48 +++++++++++++++++++ sessions/libsessions.go | 2 + 7 files changed, 100 insertions(+), 2 deletions(-) diff --git a/apier/v1/api_interfaces.go b/apier/v1/api_interfaces.go index 94435783f..ccd356474 100644 --- a/apier/v1/api_interfaces.go +++ b/apier/v1/api_interfaces.go @@ -95,6 +95,9 @@ type SessionSv1Interface interface { SetPassiveSession(args *sessions.Session, reply *string) error ActivateSessions(args *utils.SessionIDsWithArgsDispatcher, reply *string) error DeactivateSessions(args *utils.SessionIDsWithArgsDispatcher, reply *string) error + + STIRAuthenticate(args *sessions.V1STIRAuthenticateArgs, reply *string) error + STIRIdentity(args *sessions.V1STIRIdentityArgs, reply *string) error } type ResponderInterface interface { diff --git a/apier/v1/dispatcher.go b/apier/v1/dispatcher.go index 6670fe958..6266846da 100755 --- a/apier/v1/dispatcher.go +++ b/apier/v1/dispatcher.go @@ -520,6 +520,13 @@ func (dS *DispatcherSessionSv1) SyncSessions(args *utils.TenantWithArgDispatcher return dS.dS.SessionSv1SyncSessions(args, rply) } +func (dS *DispatcherSessionSv1) STIRAuthenticate(args *sessions.V1STIRAuthenticateArgs, reply *string) error { + return dS.dS.SessionSv1STIRAuthenticate(args, reply) +} +func (dS *DispatcherSessionSv1) STIRIdentity(args *sessions.V1STIRIdentityArgs, reply *string) error { + return dS.dS.SessionSv1STIRIdentity(args, reply) +} + func NewDispatcherResponder(dps *dispatchers.DispatcherService) *DispatcherResponder { return &DispatcherResponder{dS: dps} } diff --git a/data/tariffplans/dispatchers/Attributes.csv b/data/tariffplans/dispatchers/Attributes.csv index 187e4c527..371f32d75 100644 --- a/data/tariffplans/dispatchers/Attributes.csv +++ b/data/tariffplans/dispatchers/Attributes.csv @@ -9,7 +9,7 @@ cgrates.org,ATTR_API_THR_AUTH,*auth,*string:~*req.APIKey:thr12345,,,*req.APIMeth cgrates.org,ATTR_API_SUP_AUTH,*auth,*string:~*req.APIKey:sup12345,,,*req.APIMethods,*constant,SupplierSv1.Ping&SupplierSv1.GetSuppliers&SupplierSv1.GetSupplierProfilesForEvent,false,20 cgrates.org,ATTR_API_STAT_AUTH,*auth,*string:~*req.APIKey:stat12345,,,*req.APIMethods,*constant,StatSv1.Ping&StatSv1.GetStatQueuesForEvent&StatSv1.GetQueueStringMetrics&StatSv1.ProcessEvent&StatSv1.GetQueueIDs&StatSv1.GetQueueFloatMetrics,false,20 cgrates.org,ATTR_API_RES_AUTH,*auth,*string:~*req.APIKey:res12345,,,*req.APIMethods,*constant,ResourceSv1.Ping&ResourceSv1.GetResourcesForEvent&ResourceSv1.AuthorizeResources&ResourceSv1.AllocateResources&ResourceSv1.ReleaseResources&ResourceSv1.GetResource,false,20 -cgrates.org,ATTR_API_SES_AUTH,*auth,*string:~*req.APIKey:ses12345,,,*req.APIMethods,*constant,SessionSv1.Ping&SessionSv1.AuthorizeEvent&SessionSv1.AuthorizeEventWithDigest&SessionSv1.InitiateSession&SessionSv1.InitiateSessionWithDigest&SessionSv1.UpdateSession&SessionSv1.SyncSessions&SessionSv1.TerminateSession&SessionSv1.ProcessCDR&SessionSv1.ProcessMessage&SessionSv1.GetActiveSessions&SessionSv1.GetActiveSessionsCount&SessionSv1.ForceDisconnect&SessionSv1.GetPassiveSessions&SessionSv1.GetPassiveSessionsCount&SessionSv1.ReplicateSessions&SessionSv1.SetPassiveSession&SessionSv1.ProcessEvent&SessionSv1.GetCost,false,20 +cgrates.org,ATTR_API_SES_AUTH,*auth,*string:~*req.APIKey:ses12345,,,*req.APIMethods,*constant,SessionSv1.Ping&SessionSv1.AuthorizeEvent&SessionSv1.AuthorizeEventWithDigest&SessionSv1.InitiateSession&SessionSv1.InitiateSessionWithDigest&SessionSv1.UpdateSession&SessionSv1.SyncSessions&SessionSv1.TerminateSession&SessionSv1.ProcessCDR&SessionSv1.ProcessMessage&SessionSv1.GetActiveSessions&SessionSv1.GetActiveSessionsCount&SessionSv1.ForceDisconnect&SessionSv1.GetPassiveSessions&SessionSv1.GetPassiveSessionsCount&SessionSv1.ReplicateSessions&SessionSv1.SetPassiveSession&SessionSv1.ProcessEvent&SessionSv1.GetCost&SessionSv1.STIRAuthenticate&SessionSv1.STIRIdentity,false,20 cgrates.org,ATTR_API_RSP_AUTH,*auth,*string:~*req.APIKey:rsp12345,,,*req.APIMethods,*constant,CoreSv1.Status&CoreSv1.Ping&Responder.Shutdown&Responder.Ping,false,20 cgrates.org,ATTR_API_CHC_AUTH,*auth,*string:~*req.APIKey:chc12345,,,*req.APIMethods,*constant,CacheSv1.Ping&CacheSv1.GetCacheStats&CacheSv1.LoadCache&CacheSv1.PrecacheStatus&CacheSv1.GetItemIDs&CacheSv1.HasItem&CacheSv1.GetItemExpiryTime&CacheSv1.ReloadCache&CacheSv1.RemoveItem&CacheSv1.FlushCache&CacheSv1.Clear,false,20 cgrates.org,ATTR_API_GRD_AUTH,*auth,*string:~*req.APIKey:grd12345,,,*req.APIMethods,*constant,GuardianSv1.Ping&GuardianSv1.RemoteLock&GuardianSv1.RemoteUnlock,false,20 diff --git a/data/tariffplans/dispatchers_gob/Attributes.csv b/data/tariffplans/dispatchers_gob/Attributes.csv index 187e4c527..371f32d75 100644 --- a/data/tariffplans/dispatchers_gob/Attributes.csv +++ b/data/tariffplans/dispatchers_gob/Attributes.csv @@ -9,7 +9,7 @@ cgrates.org,ATTR_API_THR_AUTH,*auth,*string:~*req.APIKey:thr12345,,,*req.APIMeth cgrates.org,ATTR_API_SUP_AUTH,*auth,*string:~*req.APIKey:sup12345,,,*req.APIMethods,*constant,SupplierSv1.Ping&SupplierSv1.GetSuppliers&SupplierSv1.GetSupplierProfilesForEvent,false,20 cgrates.org,ATTR_API_STAT_AUTH,*auth,*string:~*req.APIKey:stat12345,,,*req.APIMethods,*constant,StatSv1.Ping&StatSv1.GetStatQueuesForEvent&StatSv1.GetQueueStringMetrics&StatSv1.ProcessEvent&StatSv1.GetQueueIDs&StatSv1.GetQueueFloatMetrics,false,20 cgrates.org,ATTR_API_RES_AUTH,*auth,*string:~*req.APIKey:res12345,,,*req.APIMethods,*constant,ResourceSv1.Ping&ResourceSv1.GetResourcesForEvent&ResourceSv1.AuthorizeResources&ResourceSv1.AllocateResources&ResourceSv1.ReleaseResources&ResourceSv1.GetResource,false,20 -cgrates.org,ATTR_API_SES_AUTH,*auth,*string:~*req.APIKey:ses12345,,,*req.APIMethods,*constant,SessionSv1.Ping&SessionSv1.AuthorizeEvent&SessionSv1.AuthorizeEventWithDigest&SessionSv1.InitiateSession&SessionSv1.InitiateSessionWithDigest&SessionSv1.UpdateSession&SessionSv1.SyncSessions&SessionSv1.TerminateSession&SessionSv1.ProcessCDR&SessionSv1.ProcessMessage&SessionSv1.GetActiveSessions&SessionSv1.GetActiveSessionsCount&SessionSv1.ForceDisconnect&SessionSv1.GetPassiveSessions&SessionSv1.GetPassiveSessionsCount&SessionSv1.ReplicateSessions&SessionSv1.SetPassiveSession&SessionSv1.ProcessEvent&SessionSv1.GetCost,false,20 +cgrates.org,ATTR_API_SES_AUTH,*auth,*string:~*req.APIKey:ses12345,,,*req.APIMethods,*constant,SessionSv1.Ping&SessionSv1.AuthorizeEvent&SessionSv1.AuthorizeEventWithDigest&SessionSv1.InitiateSession&SessionSv1.InitiateSessionWithDigest&SessionSv1.UpdateSession&SessionSv1.SyncSessions&SessionSv1.TerminateSession&SessionSv1.ProcessCDR&SessionSv1.ProcessMessage&SessionSv1.GetActiveSessions&SessionSv1.GetActiveSessionsCount&SessionSv1.ForceDisconnect&SessionSv1.GetPassiveSessions&SessionSv1.GetPassiveSessionsCount&SessionSv1.ReplicateSessions&SessionSv1.SetPassiveSession&SessionSv1.ProcessEvent&SessionSv1.GetCost&SessionSv1.STIRAuthenticate&SessionSv1.STIRIdentity,false,20 cgrates.org,ATTR_API_RSP_AUTH,*auth,*string:~*req.APIKey:rsp12345,,,*req.APIMethods,*constant,CoreSv1.Status&CoreSv1.Ping&Responder.Shutdown&Responder.Ping,false,20 cgrates.org,ATTR_API_CHC_AUTH,*auth,*string:~*req.APIKey:chc12345,,,*req.APIMethods,*constant,CacheSv1.Ping&CacheSv1.GetCacheStats&CacheSv1.LoadCache&CacheSv1.PrecacheStatus&CacheSv1.GetItemIDs&CacheSv1.HasItem&CacheSv1.GetItemExpiryTime&CacheSv1.ReloadCache&CacheSv1.RemoveItem&CacheSv1.FlushCache&CacheSv1.Clear,false,20 cgrates.org,ATTR_API_GRD_AUTH,*auth,*string:~*req.APIKey:grd12345,,,*req.APIMethods,*constant,GuardianSv1.Ping&GuardianSv1.RemoteLock&GuardianSv1.RemoteUnlock,false,20 diff --git a/dispatchers/sessions.go b/dispatchers/sessions.go index e0eadca56..2ac7ab402 100755 --- a/dispatchers/sessions.go +++ b/dispatchers/sessions.go @@ -482,3 +482,41 @@ func (dS *DispatcherService) SessionSv1DeactivateSessions(args *utils.SessionIDs return dS.Dispatch(&utils.CGREvent{Tenant: tnt}, utils.MetaSessionS, routeID, utils.SessionSv1DeactivateSessions, args, reply) } + +func (dS *DispatcherService) SessionSv1STIRAuthenticate(args *sessions.V1STIRAuthenticateArgs, reply *string) (err error) { + tnt := dS.cfg.GeneralCfg().DefaultTenant + if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 { + if args.ArgDispatcher == nil { + return utils.NewErrMandatoryIeMissing(utils.ArgDispatcherField) + } + if err = dS.authorize(utils.SessionSv1STIRAuthenticate, + tnt, args.APIKey, utils.TimePointer(time.Now())); err != nil { + return + } + } + var routeID *string + if args.ArgDispatcher != nil { + routeID = args.ArgDispatcher.RouteID + } + return dS.Dispatch(&utils.CGREvent{Tenant: tnt}, utils.MetaSessionS, routeID, + utils.SessionSv1STIRAuthenticate, args, reply) +} + +func (dS *DispatcherService) SessionSv1STIRIdentity(args *sessions.V1STIRIdentityArgs, reply *string) (err error) { + tnt := dS.cfg.GeneralCfg().DefaultTenant + if len(dS.cfg.DispatcherSCfg().AttributeSConns) != 0 { + if args.ArgDispatcher == nil { + return utils.NewErrMandatoryIeMissing(utils.ArgDispatcherField) + } + if err = dS.authorize(utils.SessionSv1STIRIdentity, + tnt, args.APIKey, utils.TimePointer(time.Now())); err != nil { + return + } + } + var routeID *string + if args.ArgDispatcher != nil { + routeID = args.ArgDispatcher.RouteID + } + return dS.Dispatch(&utils.CGREvent{Tenant: tnt}, utils.MetaSessionS, routeID, + utils.SessionSv1STIRIdentity, args, reply) +} diff --git a/dispatchers/sessions_it_test.go b/dispatchers/sessions_it_test.go index 49f6cf805..6f2261575 100755 --- a/dispatchers/sessions_it_test.go +++ b/dispatchers/sessions_it_test.go @@ -54,6 +54,9 @@ var sTestsDspSession = []func(t *testing.T){ testDspSessionGetCost, testDspSessionReplicate, testDspSessionPassive, + + testDspSessionSTIRAuthenticate, + testDspSessionSTIRIdentity, testDspSessionForceDisconect, } @@ -972,3 +975,48 @@ func testDspSessionGetCost(t *testing.T) { } } + +func testDspSessionSTIRAuthenticate(t *testing.T) { + var rply string + if err := dispEngine.RPC.Call(utils.SessionSv1STIRAuthenticate, + &sessions.V1STIRAuthenticateArgs{ + Attest: []string{"A"}, + PayloadMaxDuration: "-1", + DestinationTn: "1002", + Identity: "eyJhbGciOiJFUzI1NiIsInBwdCI6InNoYWtlbiIsInR5cCI6InBhc3Nwb3J0IiwieDV1IjoiL3Vzci9zaGFyZS9jZ3JhdGVzL3N0aXIvc3Rpcl9wdWJrZXkucGVtIn0.eyJhdHRlc3QiOiJBIiwiZGVzdCI6eyJ0biI6WyIxMDAyIl19LCJpYXQiOjE1ODcwMzg4MDIsIm9yaWciOnsidG4iOiIxMDAxIn0sIm9yaWdpZCI6IjEyMzQ1NiJ9.cMEMlFnfyTu8uxfeU4RoZTamA7ifFT9Ibwrvi1_LKwL2xAU6fZ_CSIxKbtyOpNhM_sV03x7CfA_v0T4sHkifzg;info=;ppt=shaken", + OriginatorTn: "1001", + ArgDispatcher: &utils.ArgDispatcher{ + APIKey: utils.StringPointer("ses12345"), + }, + }, &rply); err != nil { + t.Fatal(err) + } else if rply != utils.OK { + t.Errorf("Expected: %s ,received: %s", utils.OK, rply) + } +} + +func testDspSessionSTIRIdentity(t *testing.T) { + payload := &utils.PASSporTPayload{ + Dest: utils.PASSporTDestinationsIdentity{Tn: []string{"1002"}}, + IAT: 1587019822, + Orig: utils.PASSporTOriginsIdentity{Tn: "1001"}, + OrigID: "123456", + } + args := &sessions.V1STIRIdentityArgs{ + Payload: payload, + PublicKeyPath: "/usr/share/cgrates/stir/stir_pubkey.pem", + PrivateKeyPath: "/usr/share/cgrates/stir/stir_privatekey.pem", + OverwriteIAT: true, + ArgDispatcher: &utils.ArgDispatcher{ + APIKey: utils.StringPointer("ses12345"), + }, + } + var rply string + if err := dispEngine.RPC.Call(utils.SessionSv1STIRIdentity, + args, &rply); err != nil { + t.Error(err) + } + if err := sessions.AuthStirShaken(rply, "1001", "", "1002", "", utils.NewStringSet([]string{"A"}), 10*time.Minute); err != nil { + t.Fatal(err) + } +} diff --git a/sessions/libsessions.go b/sessions/libsessions.go index e0805eac4..1460473fa 100644 --- a/sessions/libsessions.go +++ b/sessions/libsessions.go @@ -244,6 +244,7 @@ type V1STIRAuthenticateArgs struct { OriginatorTn string // the expected originator telephone number OriginatorURI string // the expected originator URI; if this is populated the OriginatorTn is ignored PayloadMaxDuration string // the duration the payload is valid after it's creation + *utils.ArgDispatcher } // V1STIRIdentityArgs are the arguments for STIRIdentity API @@ -252,4 +253,5 @@ type V1STIRIdentityArgs struct { PublicKeyPath string // the path to the public key used in the header PrivateKeyPath string // the private key path OverwriteIAT bool // if true the IAT from payload is overwrited with the present unix timestamp + *utils.ArgDispatcher }