Debian Codegraph @

crt/aws-c-auth/source/sso_token_utils.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/auth/private/sso_token_utils.h>
7 #include <aws/cal/hash.h>
8 #include <aws/common/encoding.h>
9 #include <aws/common/file.h>
10 #include <aws/common/json.h>
11
12 #if defined(_MSC_VER)
13 # pragma warning(disable : 4232)
14 #endif /* _MSC_VER */
15
16 struct aws_string *aws_construct_sso_token_path(struct aws_allocator *allocator, const struct aws_string *input) {
17 AWS_PRECONDITION(input);
18
19 struct aws_string *sso_token_path_str = NULL;
20
21 struct aws_string *home_directory = aws_get_home_directory(allocator);
22 if (!home_directory) {
23 return NULL;
24 }
25
26 struct aws_byte_cursor home_dir_cursor = aws_byte_cursor_from_string(home_directory);
27 struct aws_byte_cursor input_cursor = aws_byte_cursor_from_string(input);
28 struct aws_byte_cursor json_cursor = aws_byte_cursor_from_c_str(".json");
29
30 struct aws_byte_buf sso_token_path_buf;
31 AWS_ZERO_STRUCT(sso_token_path_buf);
32 struct aws_byte_buf sha1_buf;
33 AWS_ZERO_STRUCT(sha1_buf);
34
35 /* append home directory */
36 if (aws_byte_buf_init_copy_from_cursor(&sso_token_path_buf, allocator, home_dir_cursor)) {
37 goto cleanup;
38 }
39
40 /* append sso cache directory */
41 struct aws_byte_cursor sso_cache_dir_cursor = aws_byte_cursor_from_c_str("/.aws/sso/cache/");
42 if (aws_byte_buf_append_dynamic(&sso_token_path_buf, &sso_cache_dir_cursor)) {
43 goto cleanup;
44 }
45
46 /* append hex encoded sha1 of input */
47 if (aws_byte_buf_init(&sha1_buf, allocator, AWS_SHA1_LEN) ||
48 aws_sha1_compute(allocator, &input_cursor, &sha1_buf, 0)) {
49 goto cleanup;
50 }
51 struct aws_byte_cursor sha1_cursor = aws_byte_cursor_from_buf(&sha1_buf);
52 if (aws_hex_encode_append_dynamic(&sha1_cursor, &sso_token_path_buf)) {
53 goto cleanup;
54 }
55
56 /* append .json */
57 if (aws_byte_buf_append_dynamic(&sso_token_path_buf, &json_cursor)) {
58 goto cleanup;
59 }
60
61 /* use platform-specific directory separator. */
62 aws_normalize_directory_separator(&sso_token_path_buf);
63
64 sso_token_path_str = aws_string_new_from_buf(allocator, &sso_token_path_buf);
65 AWS_LOGF_INFO(
66 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
67 "successfully constructed token path: %s",
68 aws_string_c_str(sso_token_path_str));
69 cleanup:
70 aws_byte_buf_clean_up(&sso_token_path_buf);
71 aws_byte_buf_clean_up(&sha1_buf);
72 aws_string_destroy(home_directory);
73 return sso_token_path_str;
74 }
75
76 void aws_sso_token_destroy(struct aws_sso_token *sso_token) {
77 if (sso_token == NULL) {
78 return;
79 }
80
81 aws_string_destroy(sso_token->access_token);
82 aws_mem_release(sso_token->allocator, sso_token);
83 }
84
85 struct aws_sso_token *aws_sso_token_new_from_file(struct aws_allocator *allocator, const struct aws_string *file_path) {
86 AWS_PRECONDITION(allocator);
87 AWS_PRECONDITION(file_path);
88
89 bool success = false;
90
91 struct aws_sso_token *token = aws_mem_calloc(allocator, 1, sizeof(struct aws_sso_token));
92 token->allocator = allocator;
93 struct aws_byte_buf file_contents_buf;
94 AWS_ZERO_STRUCT(file_contents_buf);
95 struct aws_json_value *document_root = NULL;
96
97 if (aws_byte_buf_init_from_file(&file_contents_buf, allocator, aws_string_c_str(file_path))) {
98 AWS_LOGF_ERROR(
99 AWS_LS_AUTH_CREDENTIALS_PROVIDER, "sso token: failed to load token file %s", aws_string_c_str(file_path));
100 goto cleanup;
101 }
102
103 struct aws_byte_cursor document_cursor = aws_byte_cursor_from_buf(&file_contents_buf);
104 document_root = aws_json_value_new_from_string(allocator, document_cursor);
105 if (document_root == NULL) {
106 AWS_LOGF_ERROR(
107 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
108 "sso token: failed to parse sso token file %s",
109 aws_string_c_str(file_path));
110 aws_raise_error(AWS_AUTH_SSO_TOKEN_INVALID);
111 goto cleanup;
112 }
113
114 struct aws_byte_cursor access_token_cursor;
115 struct aws_json_value *access_token =
116 aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("accessToken"));
117 if (!aws_json_value_is_string(access_token) || aws_json_value_get_string(access_token, &access_token_cursor)) {
118 AWS_LOGF_ERROR(
119 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
120 "sso token: failed to parse accessToken from %s",
121 aws_string_c_str(file_path));
122 aws_raise_error(AWS_AUTH_SSO_TOKEN_INVALID);
123 goto cleanup;
124 }
125
126 struct aws_byte_cursor expires_at_cursor;
127 struct aws_json_value *expires_at =
128 aws_json_value_get_from_object(document_root, aws_byte_cursor_from_c_str("expiresAt"));
129 if (!aws_json_value_is_string(expires_at) || aws_json_value_get_string(expires_at, &expires_at_cursor)) {
130 AWS_LOGF_ERROR(
131 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
132 "sso token: failed to parse expiresAt from %s",
133 aws_string_c_str(file_path));
134 aws_raise_error(AWS_AUTH_SSO_TOKEN_INVALID);
135 goto cleanup;
136 }
137 struct aws_date_time expiration;
138 if (aws_date_time_init_from_str_cursor(&expiration, &expires_at_cursor, AWS_DATE_FORMAT_ISO_8601)) {
139 AWS_LOGF_ERROR(
140 AWS_LS_AUTH_CREDENTIALS_PROVIDER,
141 "sso token: expiresAt '" PRInSTR "' in %s is not a valid ISO-8601 date string",
142 AWS_BYTE_CURSOR_PRI(expires_at_cursor),
143 aws_string_c_str(file_path));
144 aws_raise_error(AWS_AUTH_SSO_TOKEN_INVALID);
145 goto cleanup;
146 }
147 token->access_token = aws_string_new_from_cursor(allocator, &access_token_cursor);
148 token->expiration = expiration;
149
150 success = true;
151
152 cleanup:
153 aws_json_value_destroy(document_root);
154 aws_byte_buf_clean_up(&file_contents_buf);
155 if (!success) {
156 aws_sso_token_destroy(token);
157 token = NULL;
158 }
159 return token;
160 }