func (a *Server) RegisterSession(referenceID string) *Session { s := NewSession(referenceID) s.Secret = toolkit.RandomString(32) s.SessionID = toolkit.RandomString(32) s.ReferenceID = referenceID a.sessions[s.SessionID] = s //a.Log.Info(fmt.Sprintf("Registering new session [%s] for %s", s.SessionID, s.ReferenceID)) return s }
func NewSession(referenceid string) *Session { s := new(Session) s.SessionID = toolkit.RandomString(32) s.ReferenceID = referenceid s.Created = time.Now() s.ExpireOn = s.Created.Add(SessionLifetime()) s.Secret = toolkit.RandomString(32) return s }
func (l *LoginController) PrepareDefaultUser() (err error) { username := colonycore.GetConfig("default_username", "").(string) password := colonycore.GetConfig("default_password", "").(string) user := new(acl.User) filter := dbox.Contains("loginid", username) c, err := acl.Find(user, filter, nil) if err != nil { return } if c.Count() == 0 { user.ID = toolkit.RandomString(32) user.LoginID = username user.FullName = username user.Password = password user.Enable = true err = acl.Save(user) if err != nil { return } err = acl.ChangePassword(user.ID, password) if err != nil { return } fmt.Printf(`Default user "%s" with standard password has been created%s`, username, "\n") } return }
func (b *Broadcaster) AddNode(k *knot.WebContext) interface{} { var nodeModel struct { Subscriber string Secret string } k.GetPayload(&nodeModel) result := toolkit.NewResult() if nodeModel.Subscriber == "" { result.SetErrorTxt("Invalid subscriber info " + nodeModel.Subscriber) return result } if nodeModel.Secret != b.secret { result.SetErrorTxt("Not authorised") return result } if b.Subscibers == nil { b.Subscibers = map[string]*SubscriberInfo{} } else { if _, exist := b.Subscibers[nodeModel.Subscriber]; exist { result.SetErrorTxt(fmt.Sprintf("%s has been registered as subsriber", nodeModel.Subscriber)) return result } } si := new(SubscriberInfo) si.Address = nodeModel.Subscriber si.Protocol = pro si.Secret = toolkit.RandomString(32) b.Subscibers[nodeModel.Subscriber] = si result.Data = si.Secret k.Server.Log().Info(fmt.Sprintf("%s register as new subscriber. Current active subscibers: %d", nodeModel.Subscriber, len(b.Subscibers))) return result }
func NewToken(tokentype, reference1 string) *Token { t := new(Token) t.Secret = toolkit.RandomString(32) t.Reference1 = reference1 t.TokenType = tokentype t.Created = time.Now() t.Expiry = t.Created.Add(TokenDefaultExpiry()) return t }
func (a *UserController) SaveUser(r *knot.WebContext) interface{} { r.Config.OutputType = knot.OutputJson a.InitialSetDatabase() payload := map[string]interface{}{} err := r.GetPayload(&payload) if err != nil { return helper.CreateResult(false, nil, err.Error()) } user := payload["user"].(map[string]interface{}) groups := user["Groups"] var group []string for _, v := range groups.([]interface{}) { group = append(group, v.(string)) } fmt.Println(user["_id"].(string)) initUser := new(acl.User) id := toolkit.RandomString(32) if user["_id"].(string) == "" { initUser.ID = id } else { initUser.ID = user["_id"].(string) } initUser.LoginID = user["LoginID"].(string) initUser.FullName = user["FullName"].(string) initUser.Email = user["Email"].(string) initUser.Password = user["Password"].(string) initUser.Enable = user["Enable"].(bool) initUser.Groups = group err = acl.Save(initUser) if err != nil { return helper.CreateResult(true, nil, err.Error()) } if user["_id"].(string) == "" { err = acl.ChangePassword(initUser.ID, user["Password"].(string)) if err != nil { return helper.CreateResult(true, nil, err.Error()) } } var grant map[string]interface{} for _, p := range payload["grants"].([]interface{}) { dat := []byte(p.(string)) if err = json.Unmarshal(dat, &grant); err != nil { return helper.CreateResult(true, nil, err.Error()) } AccessID := grant["AccessID"].(string) Accessvalue := grant["AccessValue"] for _, v := range Accessvalue.([]interface{}) { switch v { case "AccessCreate": initUser.Grant(AccessID, acl.AccessCreate) case "AccessRead": initUser.Grant(AccessID, acl.AccessRead) case "AccessUpdate": initUser.Grant(AccessID, acl.AccessUpdate) case "AccessDelete": initUser.Grant(AccessID, acl.AccessDelete) case "AccessSpecial1": initUser.Grant(AccessID, acl.AccessSpecial1) case "AccessSpecial2": initUser.Grant(AccessID, acl.AccessSpecial2) case "AccessSpecial3": initUser.Grant(AccessID, acl.AccessSpecial3) case "AccessSpecial4": initUser.Grant(AccessID, acl.AccessSpecial4) } } } err = acl.Save(initUser) return helper.CreateResult(true, nil, "sukses") }