From 0157e5de5dec16e43bb1d7ddef3878333b5fd863 Mon Sep 17 00:00:00 2001
From: Alexey Lunin <alexey.lunin@vereign.com>
Date: Tue, 27 Jun 2023 16:05:37 +0300
Subject: [PATCH] add idunion basic auth

---
 .env.example                                  |  2 ++
 compose/env/holder.env                        |  4 +++-
 compose/env/holder.simple.env                 |  4 +++-
 compose/env/issuer.env                        |  4 +++-
 compose/env/issuer.simple.env                 |  4 +++-
 libs/config/src/config/ledgers.config.ts      |  2 ++
 .../interfaces/ledgers.config.interface.ts    |  2 ++
 libs/config/src/schemas/ledgers.schema.ts     |  2 ++
 libs/ledgers/src/idunion/idunion.provider.ts  | 19 ++++++++++++++-----
 9 files changed, 34 insertions(+), 9 deletions(-)

diff --git a/.env.example b/.env.example
index 7553fff1..7ff99000 100644
--- a/.env.example
+++ b/.env.example
@@ -1,5 +1,7 @@
 LEDGERS="BCOVRIN_TEST"
 IDUNION_KEY=#add if you are using IDUNION as a ledger
+IDUNION_BASIC_USER=
+IDUNION_BASIC_PASS=
 
 AGENT_PEER_URL=http://localhost:8001,ws://localhost:8002
 AGENT_NAME=EXAMPTTLE_AGENT_45
diff --git a/compose/env/holder.env b/compose/env/holder.env
index 2d6baefb..0966044f 100644
--- a/compose/env/holder.env
+++ b/compose/env/holder.env
@@ -1,5 +1,7 @@
-LEDGERS="BCOVRIN_TEST"
+LEDGERS=IDUNION
 IDUNION_KEY=
+IDUNION_BASIC_USER=
+IDUNION_BASIC_PASS=
 
 AGENT_PEER_URL=http://new.didgram.pro:6001,ws://new.didgram.pro:6002
 AGENT_NAME=DEV_AGENT_HOLDER_OCM_4 # this should be changed to company name
diff --git a/compose/env/holder.simple.env b/compose/env/holder.simple.env
index 6d8e0ac8..f04aa0a9 100644
--- a/compose/env/holder.simple.env
+++ b/compose/env/holder.simple.env
@@ -1,5 +1,7 @@
-LEDGERS="BCOVRIN_TEST"
+LEDGERS=IDUNION
 IDUNION_KEY=
+IDUNION_BASIC_USER=
+IDUNION_BASIC_PASS=
 
 AGENT_PEER_URL=http://agent-holder:6001,ws://agent-holder:6002
 AGENT_NAME=DEV_SIMPLE_AGENT_HOLDER_OCM # this should be changed to company name
diff --git a/compose/env/issuer.env b/compose/env/issuer.env
index 5ddf0c87..92b9df2b 100644
--- a/compose/env/issuer.env
+++ b/compose/env/issuer.env
@@ -1,5 +1,7 @@
-LEDGERS="BCOVRIN_TEST"
+LEDGERS=IDUNION
 IDUNION_KEY=
+IDUNION_BASIC_USER=
+IDUNION_BASIC_PASS=
 
 AGENT_PEER_URL=http://new.didgram.pro:8001,ws://new.didgram.pro:8002
 AGENT_NAME=DEV_AGENT_ISSUER_OCM_4 # this should be changed to company name
diff --git a/compose/env/issuer.simple.env b/compose/env/issuer.simple.env
index a974ad46..02e4a0bd 100644
--- a/compose/env/issuer.simple.env
+++ b/compose/env/issuer.simple.env
@@ -1,5 +1,7 @@
-LEDGERS="BCOVRIN_TEST"
+LEDGERS=IDUNION
 IDUNION_KEY=
+IDUNION_BASIC_USER=
+IDUNION_BASIC_PASS=
 
 AGENT_PEER_URL=http://agent-issuer:8001,ws://agent-issuer:8002
 AGENT_NAME=DEV_SIMPLE_AGENT_ISSUER_OCM # this should be changed to company name
diff --git a/libs/config/src/config/ledgers.config.ts b/libs/config/src/config/ledgers.config.ts
index de79f8a9..8d7c3c67 100644
--- a/libs/config/src/config/ledgers.config.ts
+++ b/libs/config/src/config/ledgers.config.ts
@@ -7,5 +7,7 @@ export const ledgersConfig = registerAs(
   (): ILedgers => ({
     ledgers: process.env["LEDGERS"]!.split(","),
     idUnionApiKey: process.env["IDUNION_KEY"]!,
+    idUnionApiBasicUser: process.env["IDUNION_BASIC_USER"]!,
+    idUnionApiBasicPass: process.env["IDUNION_BASIC_PASS"]!,
   }),
 );
diff --git a/libs/config/src/interfaces/ledgers.config.interface.ts b/libs/config/src/interfaces/ledgers.config.interface.ts
index a49c9850..c7af9b9a 100644
--- a/libs/config/src/interfaces/ledgers.config.interface.ts
+++ b/libs/config/src/interfaces/ledgers.config.interface.ts
@@ -1,4 +1,6 @@
 export interface ILedgers {
   ledgers: Array<string>;
   idUnionApiKey: string;
+  idUnionApiBasicUser: string;
+  idUnionApiBasicPass: string;
 }
diff --git a/libs/config/src/schemas/ledgers.schema.ts b/libs/config/src/schemas/ledgers.schema.ts
index d305f728..67a2cf3f 100644
--- a/libs/config/src/schemas/ledgers.schema.ts
+++ b/libs/config/src/schemas/ledgers.schema.ts
@@ -3,4 +3,6 @@ import Joi from "joi";
 export const ledgersSchema = Joi.object({
   LEDGERS: Joi.string().required(),
   IDUNION_KEY: Joi.string(),
+  IDUNION_BASIC_USER: Joi.string(),
+  IDUNION_BASIC_PASS: Joi.string(),
 });
diff --git a/libs/ledgers/src/idunion/idunion.provider.ts b/libs/ledgers/src/idunion/idunion.provider.ts
index 5c6dcf46..0a5fa67c 100644
--- a/libs/ledgers/src/idunion/idunion.provider.ts
+++ b/libs/ledgers/src/idunion/idunion.provider.ts
@@ -30,11 +30,20 @@ export class IdunionProvider implements IRegistrator {
 
     this.logger.log(`Trying to register ${did} to idunion`);
     try {
-      await axios.post(`${URL}?apiKey=${this.config.idUnionApiKey}`, {
-        role: "ENDORSER",
-        did: unqualifiedIndyDid,
-        verkey,
-      });
+      await axios.post(
+        `${URL}?apiKey=${this.config.idUnionApiKey}`,
+        {
+          role: "ENDORSER",
+          did: unqualifiedIndyDid,
+          verkey,
+        },
+        {
+          auth: {
+            username: this.config.idUnionApiBasicUser,
+            password: this.config.idUnionApiBasicPass,
+          },
+        },
+      );
 
       this.logger.log("Registration successful");
       return did;
-- 
GitLab