Github user rob05c commented on a diff in the pull request:

    
https://github.com/apache/incubator-trafficcontrol/pull/551#discussion_r115149659
  
    --- Diff: traffic_ops/experimental/auth/auth.go ---
    @@ -132,83 +150,197 @@ func InitializeDatabase(username, password, dbname, 
server string, port uint) (*
        return db, nil
     }
     
    -func handler(w http.ResponseWriter, r *http.Request) {
    +func LegacyTOLogin(login Login, legacyLoginURL string, w 
http.ResponseWriter) (*http.Response, error) {
     
    -   Logger.Println(r.Method, r.URL.Scheme, r.Host, r.URL.RequestURI())
    +   // TODO(amiry) - Legacy token expiration should be longer than JWT 
expiration
     
    -   if r.Method == "POST" {
    -           var login Login
    -           tmUserlist := []TmUser{}
    -           body, err := ioutil.ReadAll(r.Body)
    -           if err != nil {
    -                   Logger.Println("Error reading body: ", err.Error())
    -                   http.Error(w, "Error reading body: "+err.Error(), 
http.StatusBadRequest)
    -                   return
    -           }
    -           
    -           err = json.Unmarshal(body, &login)
    -           if err != nil {
    -                   Logger.Println("Invalid JSON: ", err.Error())
    -                   http.Error(w, "Invalid JSON: "+err.Error(), 
http.StatusBadRequest)
    -                   return
    -           }
    -           
    -           stmt, err := db.PrepareNamed("SELECT role,local_passwd FROM 
tm_user WHERE username=:username")
    -           if err != nil {
    -                   Logger.Println("Database error: ", err.Error())
    -                   http.Error(w, "Database error: "+err.Error(), 
http.StatusInternalServerError)
    -                   return
    -           }
    -
    -           err = stmt.Select(&tmUserlist, login)
    -           if err != nil {
    -                   Logger.Println("Database error: ", err.Error())
    -                   http.Error(w, "Database error: "+err.Error(), 
http.StatusInternalServerError)
    -                   return
    -           }
    +   legacyLogin := LegacyLogin{ login.Username, login.Password }
     
    -           hasher := sha1.New()
    -           hasher.Write([]byte(login.Password))
    -           hashedPassword := fmt.Sprintf("%x", hasher.Sum(nil))
    +   body, err := json.Marshal(legacyLogin)
    +    if err != nil {
    +           Logger.Println("JSON marshal error: ", err.Error())
    +        return nil, err
    +    }
     
    -           if len(tmUserlist) == 0 || tmUserlist[0].Password != 
string(hashedPassword) {
    -                   Logger.Printf("Invalid username/password, username %s", 
login.Username)
    -                   http.Error(w, "Invalid username/password", 
http.StatusUnauthorized)
    -                   return
    -           }
    -
    -           Logger.Printf("User %s authenticated", login.Username)
    -
    -           claims := Claims {
    -           []string{"read-ds", "write-ds", "read-cg"},     // TODO(amiry) 
- Adding hardcoded capabilities as a POC. 
    -                                                                           
                        // Need to read from TO role tables when tables are 
ready
    -           jwt.StandardClaims {
    -                   Subject: login.Username,
    -               ExpiresAt: time.Now().Add(time.Hour * 24).Unix(),   // 
TODO(amiry) - We will need to use shorter expiration, 
    -                                                                           
                                        // and use refresh tokens to extend 
access
    -           },
    -       }
    -
    -           token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
    +   req, err := http.NewRequest("POST", legacyLoginURL,  
bytes.NewBuffer(body))
    +   client := &http.Client{}
    +    resp, err := client.Do(req)
    +   if err != nil {
    +           Logger.Println("Legacy Login error: ", err.Error(), " Legacy 
URL: ", legacyLoginURL)
    +           return nil, err;
    +   }
     
    -           tokenString, err := token.SignedString([]byte(os.Args[2]))
    -           if err != nil {
    -                   Logger.Println(err.Error())
    -                   http.Error(w, err.Error(), 
http.StatusInternalServerError)
    -                   return
    -           }
    +   return resp, err
    +}
     
    -           js, err := json.Marshal(TokenResponse{Token: tokenString})
    -           if err != nil {
    -                   Logger.Println(err.Error())
    -                   http.Error(w, err.Error(), 
http.StatusInternalServerError)
    +func makeHandler(config *Config) (func(http.ResponseWriter, 
*http.Request), error) {
    +
    +   return func (w http.ResponseWriter, r *http.Request) {
    +
    +           Logger.Println(r.Method, r.URL.Scheme, r.Host, 
r.URL.RequestURI())
    +
    +           if r.Method == "POST" {
    +
    +                   var login Login
    +                   tmUserList := []TmUser{}
    +                   userRoleList := []UserRole{}
    +                   capList := []Capability{}
    +
    +                   body, err := ioutil.ReadAll(r.Body)
    +                   if err != nil {
    +                           Logger.Println("Error reading body: ", 
err.Error())
    +                           http.Error(w, "Error reading body: 
"+err.Error(), http.StatusBadRequest)
    +                           return
    +                   }
    +                   
    +                   err = json.Unmarshal(body, &login)
    +                   if err != nil {
    +                           Logger.Println("Invalid JSON: ", err.Error())
    +                           http.Error(w, "Invalid JSON: "+err.Error(), 
http.StatusBadRequest)
    +                           return
    +                   }
    +
    +                   // Get the user id and the password from tm_user, in 
order to validate the user's password
    +                   stmt, err := db.PrepareNamed("SELECT id,local_passwd 
FROM tm_user WHERE username=:username")
    +                   if err != nil {
    +                           Logger.Println("Database error: ", err.Error())
    +                           http.Error(w, "Database error: "+err.Error(), 
http.StatusInternalServerError)
    +                           return
    +                   }
    +
    +                   err = stmt.Select(&tmUserList, login)
    +                   if err != nil {
    +                           Logger.Println("Database error: ", err.Error())
    +                           http.Error(w, "Database error: "+err.Error(), 
http.StatusInternalServerError)
    +                           return
    +                   }
    +
    +           hasher := sha1.New()
    +           hasher.Write([]byte(login.Password))
    +           hashedPassword := fmt.Sprintf("%x", hasher.Sum(nil))
    +
    +                   if len(tmUserList) == 0 || tmUserList[0].Password != 
string(hashedPassword) {
    +                           Logger.Printf("Invalid username/password, 
username %s", login.Username)
    +                           http.Error(w, "Invalid username/password", 
http.StatusUnauthorized)
    +                           return
    +                   }
    +
    +                   // We have validated the user's password, now lets get 
the user's roles
    +                   stmt, err = db.PrepareNamed("SELECT role_id FROM 
user_role WHERE user_id=:id")
    +                   if err != nil {
    +                           Logger.Println("Database error: ", err.Error())
    +                           http.Error(w, "Database error: "+err.Error(), 
http.StatusInternalServerError)
    +                           return
    +                   }
    +
    +                   err = stmt.Select(&userRoleList, tmUserList[0])
    +                   if err != nil {
    +                           Logger.Println("Database error: ", err.Error())
    +                           http.Error(w, "Database error: "+err.Error(), 
http.StatusInternalServerError)
    +                           return
    +                   }
    +
    +                   rolesIds := []int{}
    +                   for _, elem := range userRoleList {
    +                           rolesIds = append(rolesIds, elem.RoleId)
    +                   }
    +
    +                   // Get user's capabilities according to the user's roles
    +                   sql, args, err := sqlx.In("SELECT cap_name FROM 
role_capability WHERE role_id IN (?)", rolesIds)
    +                   if err != nil {
    +                           Logger.Println("Database error: ", err.Error())
    +                           http.Error(w, "Database error: "+err.Error(), 
http.StatusInternalServerError)
    +                           return
    +                   }
    +
    +                   // Replace the "?" bindvar syntax with DB specific 
syntax ($1, $2, ... for PostgreSQL)
    +                   sql = db.Rebind(sql)
    +
    +                   stmt1, err := db.Preparex(sql)
    +                   if err != nil {
    +                           Logger.Println("Database error: ", err.Error())
    +                           http.Error(w, "Database error: "+err.Error(), 
http.StatusInternalServerError)
    --- End diff --
    
    See previous comment: errors must not be returned to client.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to