Fixed more org related functionality
This commit is contained in:
@@ -308,7 +308,18 @@ class AppBase:
|
||||
print("DATA: %s\n" % data)
|
||||
return parse_wrapper(data)
|
||||
|
||||
# Looks for parantheses to grab special cases within a string, e.g:
|
||||
# int(1) lower(HELLO) or length(what's the length)
|
||||
# FIXME:
|
||||
# There is an issue in here where it returns data wrong. Example:
|
||||
# Authorization=Bearer authkey
|
||||
# =
|
||||
# Authorization=Bearer authkey
|
||||
# ^ Double space.
|
||||
def parse_wrapper_start(data):
|
||||
if "(" not in data or ")" not in data:
|
||||
return data
|
||||
|
||||
newdata = []
|
||||
newstring = ""
|
||||
record = True
|
||||
@@ -337,7 +348,7 @@ class AppBase:
|
||||
if len(newstring) > 0:
|
||||
newdata.append(newstring)
|
||||
|
||||
#print(newdata)
|
||||
print("Newdata: ", newdata)
|
||||
parsedlist = []
|
||||
non_string = False
|
||||
for item in newdata:
|
||||
@@ -348,17 +359,20 @@ class AppBase:
|
||||
parsedlist.append(ret)
|
||||
|
||||
if len(parsedlist) > 0 and not non_string:
|
||||
print("Returning parsed list: ", parsedlist)
|
||||
return " ".join(parsedlist)
|
||||
elif len(parsedlist) == 1 and non_string:
|
||||
return parsedlist[0]
|
||||
else:
|
||||
#print("Casting back to string because multi: ", parsedlist)
|
||||
print("Casting back to string because multi: ", parsedlist)
|
||||
newlist = []
|
||||
for item in parsedlist:
|
||||
try:
|
||||
newlist.append(str(item))
|
||||
except ValueError:
|
||||
newlist.append("parsing_error")
|
||||
|
||||
# Does this create the issue?
|
||||
return " ".join(newlist)
|
||||
|
||||
# Parses JSON loops and such down to the item you're looking for
|
||||
@@ -989,6 +1003,7 @@ class AppBase:
|
||||
print("Normal parsing (not looping) with data %s" % value)
|
||||
value = parse_wrapper_start(value)
|
||||
|
||||
print("POST data value: %s" % value)
|
||||
params[parameter["name"]] = value
|
||||
multi_parameters[parameter["name"]] = value
|
||||
|
||||
@@ -1019,8 +1034,9 @@ class AppBase:
|
||||
#for i in range(calltimes):
|
||||
if not multiexecution:
|
||||
print("APP_SDK DONE: Starting NORMAL execution of function")
|
||||
print("Running with params %s" % params)
|
||||
newres = await func(**params)
|
||||
#print("NEWRES: ", newres)
|
||||
print("Return from execution: %s" % newres)
|
||||
if isinstance(newres, str):
|
||||
result += newres
|
||||
else:
|
||||
@@ -1101,6 +1117,7 @@ class AppBase:
|
||||
|
||||
print("Running with params %s" % baseparams)
|
||||
ret = await func(**baseparams)
|
||||
print("Return from execution: %s" % ret)
|
||||
if isinstance(ret, dict) or isinstance(ret, list):
|
||||
results.append(ret)
|
||||
json_object = True
|
||||
|
||||
+245
-158
@@ -390,6 +390,7 @@ type Hook struct {
|
||||
Status string `json:"status" datastore:"status"`
|
||||
Workflows []string `json:"workflows" datastore:"workflows"`
|
||||
Running bool `json:"running" datastore:"running"`
|
||||
OrgId string `json:"org_id" datastore:"org_id"`
|
||||
}
|
||||
|
||||
func createFileFromFile(ctx context.Context, bucket *storage.BucketHandle, remotePath, localPath string) error {
|
||||
@@ -689,47 +690,14 @@ func handleApiAuthentication(resp http.ResponseWriter, request *http.Request) (U
|
||||
authorization = authorizationArr[0]
|
||||
}
|
||||
_ = authorization
|
||||
|
||||
//if item, err := memcache.Get(ctx, authorization); err == memcache.ErrCacheMiss {
|
||||
// // Doesn't exist :(
|
||||
// log.Printf("Couldn't find %s in cache!", authorization)
|
||||
// return User{}, err
|
||||
//} else if err != nil {
|
||||
// log.Printf("Error getting item: %v", err)
|
||||
// return User{}, err
|
||||
//} else {
|
||||
// log.Printf("%#v", item.Value)
|
||||
// var Userdata User
|
||||
|
||||
// log.Printf("Deleting key %s", authorization)
|
||||
// memcache.Delete(ctx, authorization)
|
||||
// err = json.Unmarshal(item.Value, &Userdata)
|
||||
// if err == nil {
|
||||
// return Userdata, nil
|
||||
// }
|
||||
|
||||
// return User{}, err
|
||||
//}
|
||||
}
|
||||
|
||||
c, err := request.Cookie("session_token")
|
||||
if err == nil {
|
||||
//if item, err := memcache.Get(ctx, c.Value); err == memcache.ErrCacheMiss {
|
||||
// // Not in cache
|
||||
//} else if err != nil {
|
||||
// log.Printf("Error getting item: %v", err)
|
||||
//} else {
|
||||
// var Userdata User
|
||||
// err = json.Unmarshal(item.Value, &Userdata)
|
||||
// if err == nil {
|
||||
// return Userdata, nil
|
||||
// }
|
||||
//}
|
||||
|
||||
sessionToken := c.Value
|
||||
session, err := getSession(ctx, sessionToken)
|
||||
if err != nil {
|
||||
log.Printf("Session %s doesn't exist (api auth): %s", sessionToken, err)
|
||||
log.Printf("Session %s doesn't exist (session auth): %s", sessionToken, err)
|
||||
return User{}, err
|
||||
}
|
||||
|
||||
@@ -865,7 +833,7 @@ func deleteUser(resp http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
user, userErr := handleApiAuthentication(resp, request)
|
||||
userInfo, userErr := handleApiAuthentication(resp, request)
|
||||
if userErr != nil {
|
||||
log.Printf("Api authentication failed in edit workflow: %s", userErr)
|
||||
resp.WriteHeader(401)
|
||||
@@ -873,8 +841,8 @@ func deleteUser(resp http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if user.Role != "admin" {
|
||||
log.Printf("Wrong user (%s) when deleting - must be admin", user.Username)
|
||||
if userInfo.Role != "admin" {
|
||||
log.Printf("Wrong user (%s) when deleting - must be admin", userInfo.Username)
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(`{"success": false, "reason": "Must be admin"}`))
|
||||
return
|
||||
@@ -892,46 +860,57 @@ func deleteUser(resp http.ResponseWriter, request *http.Request) {
|
||||
userId = location[4]
|
||||
}
|
||||
|
||||
if userId == user.Id {
|
||||
if userId == userInfo.Id {
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(`{"success": false, "reason": "Can't deactivate yourself"}`))
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
q := datastore.NewQuery("Users").Filter("id =", userId)
|
||||
var users []User
|
||||
_, err := dbclient.GetAll(ctx, q, &users)
|
||||
foundUser, err := getUser(ctx, userId)
|
||||
if err != nil {
|
||||
log.Printf("Error getting users apikey (deleteuser): %s", err)
|
||||
log.Printf("Can't find user %s (delete user): %s", userId, err)
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(`{"success": false, "reason": "Failed getting users for verification"}`))
|
||||
resp.Write([]byte(fmt.Sprintf(`{"success": false}`)))
|
||||
return
|
||||
}
|
||||
|
||||
if len(users) != 1 {
|
||||
log.Printf("Found too many users!")
|
||||
resp.WriteHeader(500)
|
||||
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Backend error: too many or too few users with id %s: %d"}`, userId, len(users))))
|
||||
orgFound := false
|
||||
if userInfo.ActiveOrg.Id == foundUser.ActiveOrg.Id {
|
||||
orgFound = true
|
||||
} else {
|
||||
log.Printf("FoundUser: %#v", foundUser.Orgs)
|
||||
for _, item := range foundUser.Orgs {
|
||||
if item == userInfo.ActiveOrg.Id {
|
||||
orgFound = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !orgFound {
|
||||
log.Printf("User %s is admin, but can't delete users outside their own org.", userInfo.Id)
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Can't change users outside your org."}`)))
|
||||
return
|
||||
}
|
||||
|
||||
// Invert. No user deletion.
|
||||
if users[0].Active {
|
||||
users[0].Active = false
|
||||
if foundUser.Active {
|
||||
foundUser.Active = false
|
||||
} else {
|
||||
users[0].Active = true
|
||||
foundUser.Active = true
|
||||
}
|
||||
|
||||
err = setUser(ctx, &users[0])
|
||||
err = setUser(ctx, foundUser)
|
||||
if err != nil {
|
||||
log.Printf("Failed swapping active for user %s (%s)", users[0].Username, users[0].Id)
|
||||
log.Printf("Failed swapping active for user %s (%s)", foundUser, foundUser.Username, foundUser.Id)
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(fmt.Sprintf(`{"success": true"}`)))
|
||||
resp.Write([]byte(fmt.Sprintf(`{"success": false}`)))
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Successfully inverted %s", users[0].Username)
|
||||
log.Printf("Successfully inverted %s", foundUser.Username)
|
||||
|
||||
resp.WriteHeader(200)
|
||||
resp.Write([]byte(`{"success": true}`))
|
||||
@@ -1091,7 +1070,7 @@ func handleSetEnvironments(resp http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
for _, item := range newEnvironments {
|
||||
if item.OrgId == "" {
|
||||
if item.OrgId != user.ActiveOrg.Id {
|
||||
item.OrgId = user.ActiveOrg.Id
|
||||
}
|
||||
|
||||
@@ -1327,51 +1306,53 @@ func handleLogout(resp http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Check cookie
|
||||
c, err := request.Cookie("session_token")
|
||||
userInfo, err := handleApiAuthentication(resp, request)
|
||||
if err != nil {
|
||||
resp.WriteHeader(200)
|
||||
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "%s"}`, err)))
|
||||
return
|
||||
} else {
|
||||
log.Printf("Session cookie is set!")
|
||||
}
|
||||
|
||||
var Userdata User
|
||||
ctx := context.Background()
|
||||
//item, err := memcache.Get(ctx, c.Value)
|
||||
sessionToken := ""
|
||||
//// Memcache handling for logout
|
||||
//if err == nil {
|
||||
// err = json.Unmarshal(item.Value, &Userdata)
|
||||
// if err != nil {
|
||||
// log.Printf("Failed unmarshaling: %s", err)
|
||||
// resp.WriteHeader(401)
|
||||
// resp.Write([]byte(fmt.Sprintf(`{"success": false}`)))
|
||||
// return
|
||||
// }
|
||||
|
||||
// sessionToken = Userdata.Session
|
||||
//} else {
|
||||
// // Validate with User
|
||||
sessionToken = c.Value
|
||||
session, err := getSession(ctx, sessionToken)
|
||||
if err != nil {
|
||||
log.Printf("Session %s doesn't exist (logout): %s", session.Session, err)
|
||||
log.Printf("Api authentication failed in handleLogout: %s", err)
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(`{"success": false, "reason": ""}`))
|
||||
resp.Write([]byte(`{"success": false}`))
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
session, err := getSession(ctx, userInfo.Session)
|
||||
if err != nil {
|
||||
log.Printf("Session %#v doesn't exist: %s", session, err)
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(`{"success": false, "reason": "No session"}`))
|
||||
return
|
||||
}
|
||||
|
||||
// Check cookie
|
||||
//c, err := request.Cookie("session_token")
|
||||
//if err != nil {
|
||||
// resp.WriteHeader(200)
|
||||
// resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "%s"}`, err)))
|
||||
// return
|
||||
//} else {
|
||||
// log.Printf("Session cookie is set to %s!", c.Value)
|
||||
//}
|
||||
|
||||
//var Userdata User
|
||||
//ctx := context.Background()
|
||||
//sessionToken = c.Value
|
||||
//session, err := getSession(ctx, sessionToken)
|
||||
//if err != nil {
|
||||
// log.Printf("Session %s doesn't exist (logout): %s", sessionToken, err)
|
||||
// resp.WriteHeader(401)
|
||||
// resp.Write([]byte(`{"success": false, "reason": "Couldn't find your session"}`))
|
||||
// return
|
||||
//}
|
||||
|
||||
// Get session first
|
||||
// Should basically never happen
|
||||
_, err = getUser(ctx, session.Id)
|
||||
if err != nil {
|
||||
log.Printf("Username %s doesn't exist (logout): %s", session.Username, err)
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(`{"success": false, "reason": "Username and/or password is incorrect"}`))
|
||||
return
|
||||
}
|
||||
//_, err = getUser(ctx, session.Id)
|
||||
//if err != nil {
|
||||
// log.Printf("Username %s doesn't exist (logout): %s", session.Username, err)
|
||||
// resp.WriteHeader(401)
|
||||
// resp.Write([]byte(`{"success": false, "reason": "Username and/or password is incorrect"}`))
|
||||
// return
|
||||
//}
|
||||
|
||||
// Userdata = *tmpdata
|
||||
//}
|
||||
@@ -1379,7 +1360,7 @@ func handleLogout(resp http.ResponseWriter, request *http.Request) {
|
||||
// FIXME
|
||||
// Session might delete someone elses here?
|
||||
// No need to think about before possible scale..?
|
||||
err = SetSession(ctx, Userdata, "")
|
||||
err = SetSession(ctx, userInfo, "")
|
||||
if err != nil {
|
||||
log.Printf("Error removing session for: %s", err)
|
||||
resp.WriteHeader(401)
|
||||
@@ -1387,16 +1368,16 @@ func handleLogout(resp http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
err = DeleteKey(ctx, "sessions", sessionToken)
|
||||
err = DeleteKey(ctx, "sessions", userInfo.Session)
|
||||
if err != nil {
|
||||
log.Printf("Error deleting key %s for %s: %s", c.Value, Userdata.Username, err)
|
||||
log.Printf("Error deleting key %s for %s: %s", userInfo.Session, userInfo.Username, err)
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(`{"success": false, "reason": "Username and/or password is incorrect"}`))
|
||||
return
|
||||
}
|
||||
|
||||
Userdata.Session = ""
|
||||
err = setUser(ctx, &Userdata)
|
||||
userInfo.Session = ""
|
||||
err = setUser(ctx, &userInfo)
|
||||
if err != nil {
|
||||
log.Printf("Failed updating user: %s", err)
|
||||
resp.WriteHeader(401)
|
||||
@@ -1405,10 +1386,10 @@ func handleLogout(resp http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
//memcache.Delete(request.Context(), sessionToken)
|
||||
//http.SetCookie(resp, c)
|
||||
|
||||
resp.WriteHeader(200)
|
||||
resp.Write([]byte(`{"success": false, "reason": "Successfully logged out"}`))
|
||||
http.SetCookie(resp, c)
|
||||
}
|
||||
|
||||
func generateApikey(ctx context.Context, userInfo User) (User, error) {
|
||||
@@ -1471,6 +1452,8 @@ func handleUpdateUser(resp http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Should this role reflect the users' org access?
|
||||
// When you change org -> change user role
|
||||
if userInfo.Role != "admin" {
|
||||
log.Printf("%s tried to update user %s", userInfo.Username, t.UserId)
|
||||
resp.WriteHeader(401)
|
||||
@@ -1486,6 +1469,21 @@ func handleUpdateUser(resp http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
orgFound := false
|
||||
for _, item := range foundUser.Orgs {
|
||||
if item == userInfo.ActiveOrg.Id {
|
||||
orgFound = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !orgFound {
|
||||
log.Printf("User %s is admin, but can't edit users outside their own org.", userInfo.Id)
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Can't change users outside your org."}`)))
|
||||
return
|
||||
}
|
||||
|
||||
if t.Role != "admin" && t.Role != "user" {
|
||||
log.Printf("%s tried and failed to update user %s", userInfo.Username, t.UserId)
|
||||
resp.WriteHeader(401)
|
||||
@@ -1657,13 +1655,13 @@ func handleInfo(resp http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
session, err := getSession(ctx, userInfo.Session)
|
||||
if err != nil {
|
||||
log.Printf("Session %#v doesn't exist: %s", session, err)
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(`{"success": false, "reason": "No session"}`))
|
||||
return
|
||||
}
|
||||
//session, err := getSession(ctx, userInfo.Session)
|
||||
//if err != nil {
|
||||
// log.Printf("Session %#v doesn't exist: %s", session, err)
|
||||
// resp.WriteHeader(401)
|
||||
// resp.Write([]byte(`{"success": false, "reason": "No session"}`))
|
||||
// return
|
||||
//}
|
||||
|
||||
// This is a long check to see if an inactive admin can access the site
|
||||
parsedAdmin := "false"
|
||||
@@ -1726,12 +1724,12 @@ func handleInfo(resp http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
//log.Printf("%s %s", session.Session, UserInfo.Session)
|
||||
if session.Session != userInfo.Session {
|
||||
log.Printf("Session %s is not the same as %s for %s. %s", userInfo.Session, session.Session, userInfo.Username, err)
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(`{"success": false, "reason": ""}`))
|
||||
return
|
||||
}
|
||||
//if session.Session != userInfo.Session {
|
||||
// log.Printf("Session %s is not the same as %s for %s. %s", userInfo.Session, session.Session, userInfo.Username, err)
|
||||
// resp.WriteHeader(401)
|
||||
// resp.Write([]byte(`{"success": false, "reason": ""}`))
|
||||
// return
|
||||
//}
|
||||
|
||||
expiration := time.Now().Add(3600 * time.Second)
|
||||
http.SetCookie(resp, &http.Cookie{
|
||||
@@ -2015,7 +2013,7 @@ func handlePasswordChange(resp http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := handleApiAuthentication(resp, request)
|
||||
userInfo, err := handleApiAuthentication(resp, request)
|
||||
if err != nil {
|
||||
log.Printf("Api authentication failed in set new workflowhandler: %s", err)
|
||||
resp.WriteHeader(401)
|
||||
@@ -2024,15 +2022,15 @@ func handlePasswordChange(resp http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
curUserFound := false
|
||||
if t.Username != user.Username && user.Role != "admin" {
|
||||
if t.Username != userInfo.Username && userInfo.Role != "admin" {
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(`{"success": false, "reason": "Admin required to change others' passwords"}`))
|
||||
return
|
||||
} else if t.Username == user.Username {
|
||||
} else if t.Username == userInfo.Username {
|
||||
curUserFound = true
|
||||
}
|
||||
|
||||
if user.Role != "admin" {
|
||||
if userInfo.Role != "admin" {
|
||||
if t.Newpassword != t.Newpassword2 {
|
||||
err := "Passwords don't match"
|
||||
resp.WriteHeader(401)
|
||||
@@ -2046,6 +2044,8 @@ func handlePasswordChange(resp http.ResponseWriter, request *http.Request) {
|
||||
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "%s"}`, err)))
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// Check ORG HERE?
|
||||
}
|
||||
|
||||
// Current password
|
||||
@@ -2058,6 +2058,7 @@ func handlePasswordChange(resp http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
foundUser := User{}
|
||||
if !curUserFound {
|
||||
log.Printf("Have to find a different user")
|
||||
q := datastore.NewQuery("Users").Filter("Username =", strings.ToLower(t.Username))
|
||||
@@ -2077,13 +2078,32 @@ func handlePasswordChange(resp http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
user = users[0]
|
||||
foundUser = users[0]
|
||||
orgFound := false
|
||||
if userInfo.ActiveOrg.Id == foundUser.ActiveOrg.Id {
|
||||
orgFound = true
|
||||
} else {
|
||||
log.Printf("FoundUser: %#v", foundUser.Orgs)
|
||||
for _, item := range foundUser.Orgs {
|
||||
if item == userInfo.ActiveOrg.Id {
|
||||
orgFound = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !orgFound {
|
||||
log.Printf("User %s is admin, but can't change user's passowrd outside their own org.", userInfo.Id)
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Can't change users outside your org."}`)))
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// Admins can re-generate others' passwords as well.
|
||||
if user.Role != "admin" {
|
||||
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(t.Newpassword))
|
||||
if userInfo.Role != "admin" {
|
||||
err = bcrypt.CompareHashAndPassword([]byte(userInfo.Password), []byte(t.Newpassword))
|
||||
if err != nil {
|
||||
log.Printf("Bad password for %s: %s", user.Username, err)
|
||||
log.Printf("Bad password for %s: %s", userInfo.Username, err)
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(`{"success": false, "reason": "Username and/or password is incorrect"}`))
|
||||
return
|
||||
@@ -2091,18 +2111,25 @@ func handlePasswordChange(resp http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
if len(foundUser.Id) == 0 {
|
||||
log.Printf("Something went wrong in password reset", err)
|
||||
resp.WriteHeader(500)
|
||||
resp.Write([]byte(`{"success": false}`))
|
||||
return
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(t.Newpassword), 8)
|
||||
if err != nil {
|
||||
log.Printf("New password failure for %s: %s", user.Username, err)
|
||||
log.Printf("New password failure for %s: %s", userInfo.Username, err)
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(`{"success": false, "reason": "Username and/or password is incorrect"}`))
|
||||
return
|
||||
}
|
||||
|
||||
user.Password = string(hashedPassword)
|
||||
err = setUser(ctx, &user)
|
||||
userInfo.Password = string(hashedPassword)
|
||||
err = setUser(ctx, &foundUser)
|
||||
if err != nil {
|
||||
log.Printf("Error fixing password for user %s: %s", user.Username, err)
|
||||
log.Printf("Error fixing password for user %s: %s", userInfo.Username, err)
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(`{"success": false, "reason": "Username and/or password is incorrect"}`))
|
||||
return
|
||||
@@ -2341,17 +2368,15 @@ func handleGetUsers(resp http.ResponseWriter, request *http.Request) {
|
||||
|
||||
// FIXME: Check by org.
|
||||
ctx := context.Background()
|
||||
var users []User
|
||||
q := datastore.NewQuery("Users")
|
||||
_, err = dbclient.GetAll(ctx, q, &users)
|
||||
org, err := getOrg(ctx, user.ActiveOrg.Id)
|
||||
if err != nil {
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(`{"success": false, "reason": "Can't get users"}`))
|
||||
resp.Write([]byte(`{"success": false, "reason": "Failed getting org users"}`))
|
||||
return
|
||||
}
|
||||
|
||||
newUsers := []User{}
|
||||
for _, item := range users {
|
||||
for _, item := range org.Users {
|
||||
if len(item.Username) == 0 {
|
||||
continue
|
||||
}
|
||||
@@ -2458,19 +2483,10 @@ func handleLogin(resp http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("%s SUCCESSFULLY LOGGED IN with session %s", data.Username, Userdata.Session)
|
||||
//if !Userdata.Verified {
|
||||
// log.Printf("User %s is not verified", data.Username)
|
||||
// resp.WriteHeader(403)
|
||||
// resp.Write([]byte(`{"success": false, "reason": "Successful login, but your email address isn't verified. Check your mailbox."}`))
|
||||
// return
|
||||
//}
|
||||
|
||||
loginData := `{"success": true}`
|
||||
|
||||
// FIXME - have timeout here
|
||||
loginData := `{"success": true}`
|
||||
if len(Userdata.Session) != 0 {
|
||||
//log.Println("Nonexisting session")
|
||||
log.Println("User session exists - resetting")
|
||||
expiration := time.Now().Add(3600 * time.Second)
|
||||
|
||||
http.SetCookie(resp, &http.Cookie{
|
||||
@@ -2490,20 +2506,36 @@ func handleLogin(resp http.ResponseWriter, request *http.Request) {
|
||||
resp.WriteHeader(200)
|
||||
resp.Write([]byte(loginData))
|
||||
return
|
||||
} else {
|
||||
log.Printf("User session is empty - create one!")
|
||||
|
||||
sessionToken := uuid.NewV4().String()
|
||||
expiration := time.Now().Add(3600 * time.Second)
|
||||
http.SetCookie(resp, &http.Cookie{
|
||||
Name: "session_token",
|
||||
Value: sessionToken,
|
||||
Expires: expiration,
|
||||
})
|
||||
|
||||
// ADD TO DATABASE
|
||||
err = SetSession(ctx, Userdata, sessionToken)
|
||||
if err != nil {
|
||||
log.Printf("Error adding session to database: %s", err)
|
||||
}
|
||||
|
||||
Userdata.Session = sessionToken
|
||||
err = setUser(ctx, &Userdata)
|
||||
if err != nil {
|
||||
log.Printf("Failed updating user when setting session: %s", err)
|
||||
resp.WriteHeader(500)
|
||||
resp.Write([]byte(`{"success": false}`))
|
||||
return
|
||||
}
|
||||
|
||||
loginData = fmt.Sprintf(`{"success": true, "cookies": [{"key": "session_token", "value": "%s", "expiration": %d}]}`, sessionToken, expiration.Unix())
|
||||
}
|
||||
|
||||
sessionToken := uuid.NewV4()
|
||||
http.SetCookie(resp, &http.Cookie{
|
||||
Name: "session_token",
|
||||
Value: sessionToken.String(),
|
||||
Expires: time.Now().Add(3600 * time.Second),
|
||||
})
|
||||
|
||||
// ADD TO DATABASE
|
||||
err = SetSession(ctx, Userdata, sessionToken.String())
|
||||
if err != nil {
|
||||
log.Printf("Error adding session to database: %s", err)
|
||||
}
|
||||
log.Printf("%s SUCCESSFULLY LOGGED IN with session %s", data.Username, Userdata.Session)
|
||||
|
||||
resp.WriteHeader(200)
|
||||
resp.Write([]byte(loginData))
|
||||
@@ -2685,8 +2717,61 @@ func setEnvironment(ctx context.Context, data *Environment) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func fixUserOrg(ctx context.Context, user *User) *User {
|
||||
found := false
|
||||
for _, id := range user.Orgs {
|
||||
if user.ActiveOrg.Id == id {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
user.Orgs = append(user.Orgs, user.ActiveOrg.Id)
|
||||
}
|
||||
|
||||
log.Printf("Updating %d orgs for user %s", len(user.Orgs), user.Id)
|
||||
// Might be vulnerable to timing attacks.
|
||||
for _, orgId := range user.Orgs {
|
||||
if len(orgId) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
org, err := getOrg(ctx, orgId)
|
||||
if err != nil {
|
||||
log.Printf("Error getting org %s", orgId)
|
||||
continue
|
||||
}
|
||||
|
||||
orgIndex := 0
|
||||
userFound := false
|
||||
for index, orgUser := range org.Users {
|
||||
if orgUser.Id == user.Id {
|
||||
orgIndex = index
|
||||
userFound = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if userFound {
|
||||
org.Users[orgIndex] = *user
|
||||
} else {
|
||||
org.Users = append(org.Users, *user)
|
||||
}
|
||||
|
||||
err = setOrg(ctx, *org, orgId)
|
||||
if err != nil {
|
||||
log.Printf("Failed setting org %s", orgId)
|
||||
}
|
||||
}
|
||||
|
||||
return user
|
||||
}
|
||||
|
||||
// ListBooks returns a list of books, ordered by title.
|
||||
func setUser(ctx context.Context, data *User) error {
|
||||
data = fixUserOrg(ctx, data)
|
||||
|
||||
// clear session_token and API_token for user
|
||||
k := datastore.NameKey("Users", data.Id, nil)
|
||||
if _, err := dbclient.Put(ctx, k, data); err != nil {
|
||||
@@ -3481,6 +3566,7 @@ func handleNewHook(resp http.ResponseWriter, request *http.Request) {
|
||||
},
|
||||
},
|
||||
Running: false,
|
||||
OrgId: user.ActiveOrg.Id,
|
||||
}
|
||||
|
||||
hook.Status = "running"
|
||||
@@ -7027,6 +7113,7 @@ func handleCloudSetup(resp http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
// FIXME: Check if user is admin of this org
|
||||
log.Printf("Checking org %s", org.Name)
|
||||
userFound := false
|
||||
admin := false
|
||||
for _, inneruser := range org.Users {
|
||||
@@ -7061,7 +7148,7 @@ func handleCloudSetup(resp http.ResponseWriter, request *http.Request) {
|
||||
|
||||
// FIXME: Path
|
||||
client := &http.Client{}
|
||||
apiPath := "/api/v1/cloud/sync"
|
||||
apiPath := "/api/v1/cloud/sync/setup"
|
||||
if tmpData.Disable {
|
||||
if !org.CloudSync {
|
||||
log.Printf("Org %s isn't syncing. Can't stop.", org.Id)
|
||||
@@ -7072,9 +7159,9 @@ func handleCloudSetup(resp http.ResponseWriter, request *http.Request) {
|
||||
|
||||
log.Printf("Should disable sync for org %s", org.Id)
|
||||
apiPath := "/api/v1/cloud/sync/stop"
|
||||
syncUrl = fmt.Sprintf("%s%s", syncUrl, apiPath)
|
||||
syncPath := fmt.Sprintf("%s%s", syncUrl, apiPath)
|
||||
|
||||
err = handleStopCloudSync(syncUrl, *org)
|
||||
err = handleStopCloudSync(syncPath, *org)
|
||||
if err != nil {
|
||||
resp.WriteHeader(401)
|
||||
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "%s"}`, err)))
|
||||
@@ -7396,9 +7483,9 @@ func initHandlers() {
|
||||
r.HandleFunc("/api/v1/users/logout", handleLogout).Methods("POST", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/users/register", handleRegister).Methods("POST", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/users/checkusers", checkAdminLogin).Methods("GET", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/users/getusers", handleGetUsers).Methods("GET", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/users/getinfo", handleInfo).Methods("GET", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/users/getsettings", handleSettings).Methods("GET", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/users/getusers", handleGetUsers).Methods("GET", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/users/updateuser", handleUpdateUser).Methods("PUT", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/users/{user}", deleteUser).Methods("DELETE", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/users/passwordchange", handlePasswordChange).Methods("POST", "OPTIONS")
|
||||
@@ -7413,10 +7500,10 @@ func initHandlers() {
|
||||
r.HandleFunc("/api/v1/getinfo", handleInfo).Methods("GET", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/getsettings", handleSettings).Methods("GET", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/generateapikey", handleApiGeneration).Methods("GET", "POST", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/passwordchange", handlePasswordChange).Methods("POST", "OPTIONS")
|
||||
|
||||
r.HandleFunc("/api/v1/getenvironments", handleGetEnvironments).Methods("GET", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/setenvironments", handleSetEnvironments).Methods("PUT", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/passwordchange", handlePasswordChange).Methods("POST", "OPTIONS")
|
||||
|
||||
r.HandleFunc("/api/v1/docs", getDocList).Methods("GET", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/docs/{key}", getDocs).Methods("GET", "OPTIONS")
|
||||
@@ -7433,6 +7520,7 @@ func initHandlers() {
|
||||
r.HandleFunc("/api/v1/workflows/queue/confirm", handleGetWorkflowqueueConfirm).Methods("POST")
|
||||
|
||||
// App specific
|
||||
// From here down isnt checked for org specific
|
||||
r.HandleFunc("/api/v1/apps/run_hotload", handleAppHotloadRequest).Methods("GET", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/apps/get_existing", loadSpecificApps).Methods("POST", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/apps/download_remote", loadSpecificApps).Methods("POST", "OPTIONS")
|
||||
@@ -7472,7 +7560,6 @@ func initHandlers() {
|
||||
r.HandleFunc("/api/v1/workflows/{key}", deleteWorkflow).Methods("DELETE", "OPTIONS")
|
||||
|
||||
// Triggers
|
||||
// Webhook redirect to the correct cloud function
|
||||
r.HandleFunc("/api/v1/hooks/new", handleNewHook).Methods("POST", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/hooks/{key}", handleWebhookCallback).Methods("POST", "OPTIONS")
|
||||
r.HandleFunc("/api/v1/hooks/{key}/delete", handleDeleteHook).Methods("DELETE", "OPTIONS")
|
||||
|
||||
+36
-15
@@ -105,6 +105,7 @@ type AppAuthenticationStorage struct {
|
||||
Usage []AuthenticationUsage `json:"usage" datastore:"usage"`
|
||||
WorkflowCount int64 `json:"workflow_count" datastore:"workflow_count"`
|
||||
NodeCount int64 `json:"node_count" datastore:"node_count"`
|
||||
OrgId string `json:"org_id" datastore:"org_id"`
|
||||
}
|
||||
|
||||
type AuthenticationUsage struct {
|
||||
@@ -197,6 +198,7 @@ type WorkflowAppAction struct {
|
||||
}
|
||||
|
||||
// FIXME: Generate a callback authentication ID?
|
||||
// FIXME: Add org check ..
|
||||
type WorkflowExecution struct {
|
||||
Type string `json:"type" datastore:"type"`
|
||||
Status string `json:"status" datastore:"status"`
|
||||
@@ -221,6 +223,7 @@ type WorkflowExecution struct {
|
||||
Name string `json:"name" datastore:"name"`
|
||||
Value string `json:"value" datastore:"value,noindex"`
|
||||
} `json:"execution_variables,omitempty" datastore:"execution_variables,omitempty"`
|
||||
OrgId string `json:"org_id" datastore:"org_id"`
|
||||
}
|
||||
|
||||
// This is for the nodes in a workflow, NOT the app action itself.
|
||||
@@ -303,6 +306,7 @@ type Schedule struct {
|
||||
Frequency string `json:"frequency" datastore:"frequency"`
|
||||
ExecutionArgument string `json:"execution_argument" datastore:"execution_argument,noindex"`
|
||||
Id string `json:"id" datastore:"id"`
|
||||
OrgId string `json:"org_id" datastore:"org_id"`
|
||||
}
|
||||
|
||||
type Workflow struct {
|
||||
@@ -1047,6 +1051,7 @@ func handleWorkflowQueue(resp http.ResponseWriter, request *http.Request) {
|
||||
extraInputs := 0
|
||||
for _, result := range workflowExecution.Results {
|
||||
if result.Action.Name == "User Input" && result.Action.AppName == "User Input" {
|
||||
log.Printf("Found User Input node - prepare cloud?")
|
||||
extraInputs += 1
|
||||
}
|
||||
}
|
||||
@@ -1904,7 +1909,7 @@ func saveWorkflow(resp http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
allAuths, err := getAllWorkflowAppAuth(ctx)
|
||||
allAuths, err := getAllWorkflowAppAuth(ctx, user.ActiveOrg.Id)
|
||||
if userErr != nil {
|
||||
log.Printf("Api authentication failed in get all apps: %s", userErr)
|
||||
resp.WriteHeader(401)
|
||||
@@ -2575,7 +2580,7 @@ func handleExecution(id string, workflow Workflow, request *http.Request) (Workf
|
||||
// FIXME: Authentication parameters
|
||||
if len(action.AuthenticationId) > 0 {
|
||||
if len(allAuths) == 0 {
|
||||
allAuths, err = getAllWorkflowAppAuth(ctx)
|
||||
allAuths, err = getAllWorkflowAppAuth(ctx, workflow.ExecutingOrg.Id)
|
||||
if err != nil {
|
||||
log.Printf("Api authentication failed in get all app auth: %s", err)
|
||||
return WorkflowExecution{}, fmt.Sprintf("Api authentication failed in get all app auth: %s", err), err
|
||||
@@ -2786,10 +2791,18 @@ func handleExecution(id string, workflow Workflow, request *http.Request) (Workf
|
||||
return WorkflowExecution{}, "Cloud not implemented yet", errors.New("Cloud not implemented yet")
|
||||
}
|
||||
|
||||
// What it needs to know:
|
||||
// 1. Parameters
|
||||
if len(workflowExecution.Workflow.Actions) == 1 {
|
||||
log.Printf("Should execute directly with cloud instead of worker because only one action")
|
||||
cloudExecuteAction(workflowExecution.ExecutionId, workflowExecution.Workflow.Actions[0], workflowExecution.ExecutionOrg)
|
||||
return WorkflowExecution{}, "Cloud not implemented yet", errors.New("Cloud not implemented yet")
|
||||
|
||||
//cloudExecuteAction(workflowExecution.ExecutionId, workflowExecution.Workflow.Actions[0], workflowExecution.ExecutionOrg, workflowExecution.Workflow.ID)
|
||||
cloudExecuteAction(workflowExecution)
|
||||
return WorkflowExecution{}, "Cloud not implemented yet (1)", errors.New("Cloud not implemented yet")
|
||||
} else {
|
||||
// If it's here, it should be controlled by Worker.
|
||||
// If worker, should this backend be a proxy? I think so.
|
||||
return WorkflowExecution{}, "Cloud not implemented yet (2)", errors.New("Cloud not implemented yet")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2802,22 +2815,30 @@ func handleExecution(id string, workflow Workflow, request *http.Request) (Workf
|
||||
}
|
||||
|
||||
// This updates stuff locally from remote executions
|
||||
func cloudExecuteAction(workflowExecutionId string, action Action, orgId string) error {
|
||||
log.Printf("Executing action: %#v in execution ID %s", action, workflowExecutionId)
|
||||
func cloudExecuteAction(execution WorkflowExecution) error {
|
||||
ctx := context.Background()
|
||||
org, err := getOrg(ctx, orgId)
|
||||
org, err := getOrg(ctx, execution.ExecutionOrg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
type ExecutionStruct struct {
|
||||
ID string `json:"id"`
|
||||
Action Action `json:"action"`
|
||||
ExecutionId string `json:"execution_id" datastore:"execution_id"`
|
||||
Action Action `json:"action" datastore:"action"`
|
||||
Authorization string `json:"authorization" datastore:"authorization"`
|
||||
Results []ActionResult `json:"results" datastore:"results,noindex"`
|
||||
ExecutionArgument string `json:"execution_argument" datastore:"execution_argument,noindex"`
|
||||
WorkflowId string `json:"workflow_id" datastore:"workflow_id"`
|
||||
ExecutionSource string `json:"execution_source" datastore:"execution_source"`
|
||||
}
|
||||
|
||||
data := ExecutionStruct{
|
||||
ID: workflowExecutionId,
|
||||
Action: action,
|
||||
ExecutionId: execution.ExecutionId,
|
||||
WorkflowId: execution.Workflow.ID,
|
||||
Action: execution.Workflow.Actions[0],
|
||||
Authorization: execution.Authorization,
|
||||
}
|
||||
log.Printf("Executing action: %#v in execution ID %s", data.Action, data.ExecutionId)
|
||||
|
||||
b, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
@@ -3899,7 +3920,7 @@ func getAppAuthentication(resp http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
_, userErr := handleApiAuthentication(resp, request)
|
||||
user, userErr := handleApiAuthentication(resp, request)
|
||||
if userErr != nil {
|
||||
log.Printf("Api authentication failed in get all apps: %s", userErr)
|
||||
resp.WriteHeader(401)
|
||||
@@ -3915,7 +3936,7 @@ func getAppAuthentication(resp http.ResponseWriter, request *http.Request) {
|
||||
// return
|
||||
//}
|
||||
ctx := context.Background()
|
||||
allAuths, err := getAllWorkflowAppAuth(ctx)
|
||||
allAuths, err := getAllWorkflowAppAuth(ctx, user.ActiveOrg.Id)
|
||||
if err != nil {
|
||||
log.Printf("Api authentication failed in get all app auth: %s", err)
|
||||
resp.WriteHeader(401)
|
||||
@@ -5469,9 +5490,9 @@ func getAllWorkflowApps(ctx context.Context) ([]WorkflowApp, error) {
|
||||
return allworkflowapps, nil
|
||||
}
|
||||
|
||||
func getAllWorkflowAppAuth(ctx context.Context) ([]AppAuthenticationStorage, error) {
|
||||
func getAllWorkflowAppAuth(ctx context.Context, OrgId string) ([]AppAuthenticationStorage, error) {
|
||||
var allworkflowapps []AppAuthenticationStorage
|
||||
q := datastore.NewQuery("workflowappauth")
|
||||
q := datastore.NewQuery("workflowappauth").Filter("org_id = ", OrgId)
|
||||
|
||||
_, err := dbclient.GetAll(ctx, q, &allworkflowapps)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user