Ejemplo n.º 1
0
        public override string ToString()
        {
            var sb = new StringBuilder();

            foreach (var item in this)
            {
                sb.Append(SdkUtils.EncodeParam(item));
                sb.Append(Delimiter);
            }

            // remove last delimiter
            if (Count > 1)
            {
                sb.Remove(sb.Length - Delimiter.Length, Delimiter.Length);
            }

            return(sb.ToString());
        }
Ejemplo n.º 2
0
        private async Task <object> _login(string sudoId = null)
        {
            await SudoLogout();

            if (sudoId != SudoId)
            {
                SudoId = sudoId;
            }

            if (!_authToken.IsActive())
            {
                Reset();
                // only retain client API3 credentials for the lifetime of the login request
                var section  = Settings.ReadConfig();
                var clientId = section["client_id"].ToString();
                var secret   = section["client_secret"].ToString();
                if (clientId.IsNullOrEmpty() || secret.IsNullOrEmpty())
                {
                    throw new SdkError("API3 credentials client_id and/or client_secret are not set");
                }

                var body     = $"client_id={SdkUtils.EncodeParam(clientId)}&client_secret={SdkUtils.EncodeParam(secret)}";
                var apiToken = await Ok(Transport.Request <AccessToken, Exception>(
                                            HttpMethod.Post,
                                            $"{_apiPath}/login",
                                            null,
                                            body
                                            ));

                this._authToken.SetToken(apiToken);
            }

            if (sudoId.IsNullOrEmpty())
            {
                return(this.ActiveToken);
            }
            // Now that API user is logged in, login as the Sudo user
            var token = this.ActiveToken;

            async Task <HttpRequestMessage> Auth(HttpRequestMessage request)
            {
                if (token.access_token.IsFull())
                {
                    request.Headers.Authorization = new AuthenticationHeaderValue(
                        Constants.Bearer, token.access_token);
                }

                return(await Task.FromResult(request));
            }

            var sudoPath  = $"{_apiPath}/login/{SdkUtils.EncodeParam(sudoId)}";
            var sudoToken = await Ok(Transport.Request <AccessToken, Exception>(
                                         HttpMethod.Post,
                                         sudoPath,
                                         null,
                                         null,
                                         Auth
                                         ));

            _sudoToken.SetToken(sudoToken);
            return(ActiveToken);
        }