private RefreshResult GetNewSessionCredentials()

in aliyun-net-credentials/Provider/OIDCRoleArnCredentialProvider.cs [234:300]


        private RefreshResult<CredentialModel> GetNewSessionCredentials(IConnClient client)
        {
            var oidcToken = AuthUtils.GetOIDCToken(oidcTokenFilePath);
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.SetCommonUrlParameters();
            httpRequest.AddUrlParameter("Action", "AssumeRoleWithOIDC");
            httpRequest.AddUrlParameter("Format", "JSON");
            httpRequest.AddUrlParameter("Version", "2015-04-01");
            var body = new Dictionary<string, string>
            {
                { "DurationSeconds", durationSeconds.ToString() },
                { "RoleArn", roleArn },
                { "OIDCProviderArn", oidcProviderArn },
                { "OIDCToken", oidcToken },
                { "RoleSessionName", roleSessionName },
                { "Policy", policy },
            };
            bool first = true;
            var content = new StringBuilder();
            foreach (var entry in body)
            {
                if (string.IsNullOrEmpty(entry.Value))
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    content.Append("&");
                }
                content.Append(System.Web.HttpUtility.UrlEncode(entry.Key));
                content.Append("=");
                content.Append(System.Web.HttpUtility.UrlEncode(entry.Value));
            }
            httpRequest.SetHttpContent(Encoding.UTF8.GetBytes(content.ToString()), "UTF-8", FormatType.Form);
            httpRequest.Method = MethodType.POST;
            httpRequest.ConnectTimeout = connectTimeout;
            httpRequest.ReadTimeout = readTimeout;
            httpRequest.Url = ParameterHelper.ComposeUrl(STSEndpoint, httpRequest.UrlParameters, "https");
            HttpResponse httpResponse = client.DoAction(httpRequest);
            Dictionary<string, object> map =
                JsonConvert.DeserializeObject<Dictionary<string, object>>(httpResponse.GetHttpContentString());
            if (map.ContainsKey("Credentials"))
            {
                string credentialsJson = JsonConvert.SerializeObject(DictionaryUtil.Get(map, "Credentials"));
                Dictionary<string, string> credentials =
                    JsonConvert.DeserializeObject<Dictionary<string, string>>(credentialsJson);
                string expirationStr = DictionaryUtil.Get(credentials, "Expiration").Replace('T', ' ').Replace('Z', ' ');
                var dt = Convert.ToDateTime(expirationStr);
                long expiration = dt.GetTimeMillis();
                CredentialModel credentialModel = new CredentialModel
                {
                    AccessKeyId = DictionaryUtil.Get(credentials, "AccessKeyId"),
                    AccessKeySecret = DictionaryUtil.Get(credentials, "AccessKeySecret"),
                    SecurityToken = DictionaryUtil.Get(credentials, "SecurityToken"),
                    Expiration = expiration,
                    Type = AuthConstant.OIDCRoleArn,
                    ProviderName = GetProviderName()
                };
                return new RefreshResult<CredentialModel>(credentialModel, GetStaleTime(expiration));
            }

            throw new CredentialException(JsonConvert.SerializeObject(map));
        }