Skip to content

Commit

Permalink
migration: add protected.oauthstore
Browse files Browse the repository at this point in the history
  • Loading branch information
requilence committed Nov 20, 2019
1 parent 5692038 commit 9c6c1a6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
44 changes: 44 additions & 0 deletions migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package integram

import (
"time"

"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)

func migrations(db *mgo.Database, serviceName string) error {
err := migrateMissingOAuthStores(db, serviceName)
if err != nil {
return err
}

return nil
}

func migrateMissingOAuthStores(db *mgo.Database, serviceName string) error {
name := "MissingOAuthStores"
n, _ := db.C("migrations").FindId(serviceName + "_" + name).Count()
if n > 0 {
return nil
}

err := db.C("users").Update(bson.M{
"protected." + serviceName + ".oauthtoken": bson.M{"$exists": true, "$ne": ""},
"$or": []bson.M{
{"protected." + serviceName + ".oauthstore": bson.M{"$exists": false}},
{"protected." + serviceName + ".oauthstore": ""},
},
},
bson.M{"$set": bson.M{
"protected." + serviceName + ".oauthstore": "default",
"protected." + serviceName + ".oauthvalid": true,

}})

if err != nil && err != mgo.ErrNotFound {
return err
}

return db.C("migrations").Insert(bson.M{"_id": serviceName + "_" + name, "date": time.Now()})
}
9 changes: 7 additions & 2 deletions services.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,13 @@ func (s *Service) trimFuncPath(fullPath string) string{
// Register the service's config and corresponding botToken
func Register(servicer Servicer, botToken string) {
//jobs.Config.Db.Address="192.168.1.101:6379"

db := mongoSession.Clone().DB(mongo.Database)
service := servicer.Service()
err := migrations(db, service.Name)
if err != nil {
log.Fatalf("failed to apply migrations: %s", err.Error())
}

if service.DefaultOAuth1 != nil {
if service.DefaultOAuth1.AccessTokenReceiver == nil {
err := errors.New("OAuth1 need an AccessTokenReceiver func to be specified\n")
Expand Down Expand Up @@ -525,7 +530,7 @@ func Register(servicer Servicer, botToken string) {
return
}

err := service.registerBot(botToken)
err = service.registerBot(botToken)
if err != nil {
log.WithError(err).WithField("token", botToken).Panic("Can't register the bot")
}
Expand Down
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ type userProtected struct {
OAuthRefreshToken string
AuthTempToken string // Temp token for redirect to time-limited Oauth URL to authorize the user (F.e. Trello)
OAuthValid bool // used for stat purposes
OAuthStore string // to detect whether non-standard store used

AfterAuthHandler string // Used to store function that will be called after successful auth. F.e. in case of interactive reply in chat for non-authed user
AfterAuthData []byte // Gob encoded arg's
Expand Down

0 comments on commit 9c6c1a6

Please sign in to comment.