utils_ext.cc 1.55 KB
Newer Older
1
// ============================================================================
Diego Molteni's avatar
Diego Molteni committed
2
// Copyright 2017-2021, Schlumberger
Sid Stenersen's avatar
Sid Stenersen committed
3
//
4
5
6
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Sid Stenersen's avatar
Sid Stenersen committed
7
//
8
//      http://www.apache.org/licenses/LICENSE-2.0
Sid Stenersen's avatar
Sid Stenersen committed
9
//
10
11
12
13
14
15
16
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ============================================================================

Sid Stenersen's avatar
Sid Stenersen committed
17
18
19
20
#ifdef _MSC_VER
    #define NO_DLL_PUBLIC
#endif

Diego Molteni's avatar
Diego Molteni committed
21
#include "base64.h"
Sid Stenersen's avatar
Sid Stenersen committed
22
#include "json/jsonutils.h"
23

Diego Molteni's avatar
Diego Molteni committed
24
#include "utils.h"
25

Sid Stenersen's avatar
Sid Stenersen committed
26
27
namespace
{
Diego Molteni's avatar
Diego Molteni committed
28
29
    static const std::string SD_LINUX_ENV_HOME{"HOME"};
    static const std::string SD_WIN_ENV_HOME{"USERPROFILE"};
30
31
}

Sid Stenersen's avatar
Sid Stenersen committed
32
33
34
35
36
37
38
namespace seismicdrive
{
    namespace sdutils
    {
        uint64_t getAuthTokenExpiration(const std::string &authToken, const std::string &tag)
        {
            std::string payload;
Sid Stenersen's avatar
Sid Stenersen committed
39
            auto s = sdutils::split(authToken, '.', false);
Sid Stenersen's avatar
Sid Stenersen committed
40
41
42
43
44
45
            if (s.size() > 1)
            {
                payload = s[1];
            }
            payload.append((4 - payload.length() % 4) % 4, '=');
            payload = Base64UrlDecode(payload);
Sid Stenersen's avatar
Sid Stenersen committed
46
47
            auto root = jsonutils::toJson(payload, tag);
            return jsonutils::getU64(root, "exp");
Sid Stenersen's avatar
Sid Stenersen committed
48
        }
Diego Molteni's avatar
Diego Molteni committed
49
    }
50
}