/// <summary> /// Login client, and return bearer token /// </summary> /// <returns>The login.</returns> /// <param name="baseTask">Base task.</param> public static ITaskItem Login(this BaseTask baseTask, TapSecurityJson tapSecurity) { LoginResponseDto token; //authenticate try { using (WebClient client = new WebClient()) { var tokenUrl = String.Concat(baseTask.TapSettings.GetMetadata(MetadataType.TapEndpoint), Consts.TokenEndpoint); System.Collections.Specialized.NameValueCollection postData = null; if (String.IsNullOrEmpty(tapSecurity.ServiceUserAccessKey)) { postData = new System.Collections.Specialized.NameValueCollection() { { "username", tapSecurity.Username }, { "password", tapSecurity.Password }, { "grant_type", "password" }, { "scope", "openid email plantype profile offline_access roles" }, { "resource", "loadremotebuildconfig" } }; baseTask.LogDebug("Using grant_type: password"); } else { postData = new System.Collections.Specialized.NameValueCollection() { { "password", tapSecurity.ServiceUserAccessKey }, { "grant_type", "access_key" }, { "scope", "openid email plantype profile offline_access roles" }, { "resource", "loadremotebuildconfig" } }; baseTask.LogDebug("Using grant_type: access_key"); } var tokenResult = Encoding.UTF8.GetString(client.UploadValues(tokenUrl, postData)); token = JsonConvert.DeserializeObject <LoginResponseDto>(tokenResult); //client.Credentials = new NetworkCredential(securityConfig.UserName, securityConfig.Password); //var tokenResult = client.DownloadString(tokenUrl); baseTask.LogDebug("Token result recieved <-- value removed from log -->"); return(new TaskItem(token.access_token)); } } catch (Exception ex) { baseTask.Log.LogErrorFromException(ex); return(null); } }
public static TapSecurityJson GetSecurity(this BaseTask baseTask) { baseTask.LogDebug($"Loading {Consts.TapSecurityFile} file"); try { var tapSecurityPath = baseTask.FindTopFileInProjectDir(Consts.TapSecurityFile); TapSecurityJson tapSecurity = null; if (String.IsNullOrEmpty(tapSecurityPath)) { baseTask.Log.LogError($"{Consts.TapSecurityFile} file not found"); return(null); } else { var json = File.ReadAllText(tapSecurityPath); tapSecurity = JsonConvert.DeserializeObject <TapSecurityJson>(json); if ((String.IsNullOrEmpty(tapSecurity.Username) || String.IsNullOrEmpty(tapSecurity.Password)) && String.IsNullOrEmpty(tapSecurity.ServiceUserAccessKey)) { baseTask.Log.LogError($"{Consts.TapSecurityFile} username, password, or service user access key is null, please complete username and password, or service user access key at {tapSecurityPath} and restart build process"); return(null); } else { baseTask.LogDebug($"{Consts.TapSecurityFile} file read from {tapSecurityPath}, Username {tapSecurity.Username}"); return(tapSecurity); } } } catch (Exception ex) { baseTask.Log.LogErrorFromException(ex); } return(null); }