From 888a9f716b8a01e09369a7fbefa5a76e4fb8cf8e Mon Sep 17 00:00:00 2001 From: Kim Date: Thu, 23 Mar 2023 16:57:50 +0100 Subject: [PATCH] added token generator and getting token from token.json --- .../App/OpenVpnCertificatesServer/Program.cs | 18 ++++++++------ .../App/OpenVpnCertificatesServer/newToken.sh | 24 ++++++++++++++----- .../App/OpenVpnCertificatesServer/token.json | 1 + 3 files changed, 30 insertions(+), 13 deletions(-) mode change 100644 => 100755 csharp/App/OpenVpnCertificatesServer/newToken.sh create mode 100644 csharp/App/OpenVpnCertificatesServer/token.json diff --git a/csharp/App/OpenVpnCertificatesServer/Program.cs b/csharp/App/OpenVpnCertificatesServer/Program.cs index 54b426516..436a5ff6d 100644 --- a/csharp/App/OpenVpnCertificatesServer/Program.cs +++ b/csharp/App/OpenVpnCertificatesServer/Program.cs @@ -1,6 +1,5 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; -using System.Net; using System.Text; using System.Text.Json; using Flurl; @@ -125,7 +124,7 @@ public static class Program Console.WriteLine($"ccd name {ccdName} is already in use\n"); return InvalidRequest; } - + return ps.Contains(NameQueryParam) ? ParseManualRequest(ps, ccdName!) : ps.Contains(UniqueIdQueryParam) ? await LookupInstallationNameByUniqueId(ccdName!) : ps.Contains(MachineSerialQueryParam) ? await LookupInstallationNameByMachineSerial(ccdName!) @@ -138,7 +137,7 @@ public static class Program //var installationName = await LookupInstallationNameByUniqueId(ccdName); - using var vrm = VrmAccount.Token(AccToken.Id, AccToken.Token); + using var vrm = VrmAccount.Token(AccToken.idUser, AccToken.token); var installations = await vrm.GetInstallations(); var installationName = installations @@ -154,8 +153,8 @@ public static class Program private static async Task<(String ccdName, String humanReadableName)?> LookupInstallationNameByMachineSerial(String ccdName) { Console.WriteLine($"looking up {ccdName} on VRM"); - - using var vrm = VrmAccount.Token(AccToken.Id, AccToken.Token); + + using var vrm = VrmAccount.Token(AccToken.idUser, AccToken.token); var installations = await vrm.GetInstallations(); foreach (var installation in installations) @@ -273,6 +272,11 @@ public static class Program internal interface AccToken { - public String Token { get; init; } - public UInt64 Id { get; init; } + public String token { get; init; } + public String bearer { get; init; } + public UInt64 idUser { get; init; } + public String verification_mode { get; init; } + public String idAccessToken { get; init; } + public Boolean verification_sent { get; init; } + public Boolean success { get; init; } } \ No newline at end of file diff --git a/csharp/App/OpenVpnCertificatesServer/newToken.sh b/csharp/App/OpenVpnCertificatesServer/newToken.sh old mode 100644 new mode 100755 index beda996eb..31f5fa90a --- a/csharp/App/OpenVpnCertificatesServer/newToken.sh +++ b/csharp/App/OpenVpnCertificatesServer/newToken.sh @@ -2,18 +2,30 @@ ## USAGE: ./newToken.sh -u USERNAME -p PASSWORD -while getopts u:p: flag +while getopts u:p:n: flag do case "${flag}" in u) username=${OPTARG};; p) password=${OPTARG};; + n) name=${OPTARG};; esac done -curl --request POST \ +response=$(curl --request POST \ --url https://vrmapi.victronenergy.com/v2/auth/login \ --header 'Content-Type: application/json' \ - --data '{ - "username": '$username', - "password": '$password', -}' > token.json \ No newline at end of file + --data '{"username":"'$username'","password":"'$password'","sms_token":null,"remember_me":false,"language":""}') + +echo -n '{"bearer' > token.json +token=$(echo $response | jq ".token") +#echo ${token:1:-1} +uid=$(echo $response | jq ".idUser") +echo -n ${response:7:-1} >> token.json + +response2=$(curl --request POST \ + --url https://vrmapi.victronenergy.com/v2/users/$uid/accesstokens/create \ + --header 'Content-Type: application/json' \ + --header 'x-authorization: Bearer '${token:1:-1} \ + --data '{"name":"'$name'"}') +echo -n , >> token.json +echo -n ${response2:1} >> token.json \ No newline at end of file diff --git a/csharp/App/OpenVpnCertificatesServer/token.json b/csharp/App/OpenVpnCertificatesServer/token.json new file mode 100644 index 000000000..456c09453 --- /dev/null +++ b/csharp/App/OpenVpnCertificatesServer/token.json @@ -0,0 +1 @@ +{"bearer":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjJmYzY5NTNiMjhjNTg3NWQyMzQwNzM3ZjlhNGIzM2RjIn0.eyJ1aWQiOiI1NTQ1MCIsInRva2VuX3R5cGUiOiJkZWZhdWx0IiwiaXNzIjoidnJtYXBpLnZpY3Ryb25lbmVyZ3kuY29tIiwiYXVkIjoiaHR0cHM6Ly92cm1hcGkudmljdHJvbmVuZXJneS5jb20vIiwiaWF0IjoxNjc5NTg2ODU0LCJleHAiOjE2Nzk2NzMyNTQsImp0aSI6IjJmYzY5NTNiMjhjNTg3NWQyMzQwNzM3ZjlhNGIzM2RjIn0.AsLJU7qDPBHO-_FjVo9a8RbyoxhYrDrwZX7V3z4Xq8EoUulv2VyTqy9OXLpez4JI2FVAfSO5a7Amj4XvK1AWtDr9MxP07IBfPyMu5LLGTzjPMAJ6fvZbvZ-eRsP1-aARCFekMGoeSvEEBDrZB9_0kps4h9idQwwGwAby2Tya0vNGu6QEw9WmHcbL8qjvJzxavg8bK6Lttv2-3l_11iZoqiYLdXbwBi32GYK_sdwp-fbGvPE1d6g6eVll94JfSqNLZl9baijtOksx_Qouu7YB8knCgFNrx535d4iJtCkMv9xWztWXbevpSQiy9S8pCgLSpmHNztlVDjacEYyduwUzyw","idUser":55450,"verification_mode":"password","verification_sent":false,"success":true,"token":"108a1407da84afea182b8102a202a2c6b73e9bd6ad919f521033b09837e6e564","idAccessToken":"385119"} \ No newline at end of file