Skip to content
This repository has been archived by the owner on Feb 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #424 from tharindulak/master
Browse files Browse the repository at this point in the history
Corrected auth fail redirect url and added ping to listen from api
  • Loading branch information
nadundesilva committed Jun 25, 2019
2 parents b89ed77 + 64a4d2c commit 5a4f4d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
8 changes: 5 additions & 3 deletions components/cli/pkg/commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ func RunLogin(registryURL string, username string, password string) {
isCredentialsAlreadyPresent = err == nil && registryCredentials.Username != "" &&
registryCredentials.Password != ""
}

isAuthorized := make(chan bool)
if isCredentialsProvided {
fmt.Println("Logging in with provided Credentials")
} else if isCredentialsAlreadyPresent {
fmt.Println("Logging in with existing Credentials")
} else {
if password == "" {
if username == "" {
registryCredentials.Username, registryCredentials.Password, err = credentials.FromBrowser(username)
registryCredentials.Username, registryCredentials.Password, err = credentials.FromBrowser(username,
isAuthorized)
} else {
registryCredentials.Username, registryCredentials.Password, err = credentials.FromTerminal(username)
}
Expand All @@ -83,6 +84,7 @@ func RunLogin(registryURL string, username string, password string) {
spinner := util.StartNewSpinner("Logging into Cellery Registry " + registryURL)
_, err = registry.New("https://"+registryURL, registryCredentials.Username, registryCredentials.Password)
if err != nil {
isAuthorized <- false
spinner.Stop(false)
if strings.Contains(err.Error(), "401") {
util.ExitWithErrorMessage("Invalid Credentials", err)
Expand All @@ -101,7 +103,7 @@ func RunLogin(registryURL string, username string, password string) {
util.ExitWithErrorMessage("Error occurred while saving Credentials", err)
}
}

isAuthorized <- true
spinner.Stop(true)
util.PrintSuccessMessage(fmt.Sprintf("Successfully logged into Registry: %s", util.Bold(registryURL)))
}
36 changes: 22 additions & 14 deletions components/cli/pkg/registry/credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const callBackUrlContext = "/auth"
const callBackUrl = "http://localhost:%d" + callBackUrlContext

// FromBrowser requests the credentials from the user
func FromBrowser(username string) (string, string, error) {
func FromBrowser(username string, isAutherized chan bool) (string, string, error) {
conf := config.LoadConfig()
timeout := make(chan bool)
ch := make(chan string)
Expand Down Expand Up @@ -73,20 +73,28 @@ func FromBrowser(username string) (string, string, error) {
util.ExitWithErrorMessage("Error parsing the code", err)
}
code = r.Form.Get("code")
ch <- code
if len(code) != 0 {
http.Redirect(w, r, conf.Hub.Url+"/sdk/auth-success", http.StatusSeeOther)
} else {
util.ExitWithErrorMessage("Did not receive any code", err)
ping := r.Form.Get("ping")
if ping == "true" {
w.WriteHeader(http.StatusOK)
}
flusher, ok := w.(http.Flusher)
if !ok {
util.ExitWithErrorMessage("Error in casting the flusher", err)
}
flusher.Flush()
err = server.Shutdown(context.Background())
if err != nil {
util.ExitWithErrorMessage("Error while shutting down the server\n", err)
if code != "" {
ch <- code
authorized := <- isAutherized
if authorized {
http.Redirect(w, r, conf.Hub.Url+"/sdk/auth-success", http.StatusSeeOther)
} else {
// todo add authentication fail url
fmt.Println("\n\U0000274C Failed to authenticate")
}
flusher, ok := w.(http.Flusher)
if !ok {
util.ExitWithErrorMessage("Error in casting the flusher", err)
}
flusher.Flush()
err = server.Shutdown(context.Background())
if err != nil {
util.ExitWithErrorMessage("Error while shutting down the server\n", err)
}
}
})
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
Expand Down

0 comments on commit 5a4f4d5

Please sign in to comment.