Merge branch 'master' into delrepo

This commit is contained in:
Ulrich Schreiner 2015-02-05 14:47:35 +01:00
commit 05b6dad81e
11 changed files with 48 additions and 21 deletions

View file

@ -266,7 +266,11 @@ func (r *Gitlab) OpenRegistration() bool {
} }
func (r *Gitlab) GetToken(user *model.User) (*model.Token, error) { func (r *Gitlab) GetToken(user *model.User) (*model.Token, error) {
expiry := time.Now().Truncate(7200 * time.Second) expiry := time.Unix(user.TokenExpiry, 0)
if expiry.Sub(time.Now()) > (60 * time.Second) {
return nil, nil
}
t := &oauth.Transport{ t := &oauth.Transport{
Config: NewOauthConfig(r, ""), Config: NewOauthConfig(r, ""),
Token: &oauth.Token{ Token: &oauth.Token{
@ -283,5 +287,6 @@ func (r *Gitlab) GetToken(user *model.User) (*model.Token, error) {
var token = new(model.Token) var token = new(model.Token)
token.AccessToken = t.Token.AccessToken token.AccessToken = t.Token.AccessToken
token.RefreshToken = t.Token.RefreshToken token.RefreshToken = t.Token.RefreshToken
token.Expiry = t.Token.Expiry.Unix()
return token, nil return token, nil
} }

View file

@ -38,6 +38,7 @@ func Connect(driver, datasource string) (*sql.DB, error) {
var migrations = []migration.Migrator{ var migrations = []migration.Migrator{
migrate.Setup, migrate.Setup,
migrate.Migrate_20142110, migrate.Migrate_20142110,
migrate.Migrate_20152701,
} }
return migration.Open(driver, datasource, migrations) return migration.Open(driver, datasource, migrations)
} }

View file

@ -39,6 +39,20 @@ func Migrate_20142110(tx migration.LimitedTx) error {
return nil return nil
} }
// Migrate_20142110 is a database migration on Oct-10 2014.
func Migrate_20152701(tx migration.LimitedTx) error {
var stmts = []string{
addUserTokenExpires, // index the commit table repo_id column
}
for _, stmt := range stmts {
_, err := tx.Exec(transform(stmt))
if err != nil {
return err
}
}
return nil
}
var userTable = ` var userTable = `
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
user_id INTEGER PRIMARY KEY AUTOINCREMENT user_id INTEGER PRIMARY KEY AUTOINCREMENT
@ -144,3 +158,7 @@ CREATE TABLE IF NOT EXISTS blobs (
,UNIQUE(blob_path) ,UNIQUE(blob_path)
); );
` `
var addUserTokenExpires = `
ALTER TABLE users ADD COLUMN user_access_expires INTEGER
`

View file

@ -103,6 +103,7 @@ func PostCommit(c web.C, w http.ResponseWriter, r *http.Request) {
if user_token != nil { if user_token != nil {
owner.Access = user_token.AccessToken owner.Access = user_token.AccessToken
owner.Secret = user_token.RefreshToken owner.Secret = user_token.RefreshToken
owner.TokenExpiry = user_token.Expiry
datastore.PutUser(ctx, owner) datastore.PutUser(ctx, owner)
} else if err != nil { } else if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)

View file

@ -81,6 +81,7 @@ func PostHook(c web.C, w http.ResponseWriter, r *http.Request) {
if user_token != nil { if user_token != nil {
user.Access = user_token.AccessToken user.Access = user_token.AccessToken
user.Secret = user_token.RefreshToken user.Secret = user_token.RefreshToken
user.TokenExpiry = user_token.Expiry
datastore.PutUser(ctx, user) datastore.PutUser(ctx, user)
} else if err != nil { } else if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)

View file

@ -90,6 +90,7 @@ func GetLogin(c web.C, w http.ResponseWriter, r *http.Request) {
u.Access = login.Access u.Access = login.Access
u.Secret = login.Secret u.Secret = login.Secret
u.Name = login.Name u.Name = login.Name
u.TokenExpiry = login.Expiry
u.SetEmail(login.Email) u.SetEmail(login.Email)
u.Syncing = u.IsStale() u.Syncing = u.IsStale()

View file

@ -147,6 +147,7 @@ func PostRepo(c web.C, w http.ResponseWriter, r *http.Request) {
if user_token != nil { if user_token != nil {
user.Access = user_token.AccessToken user.Access = user_token.AccessToken
user.Secret = user_token.RefreshToken user.Secret = user_token.RefreshToken
user.TokenExpiry = user_token.Expiry
datastore.PutUser(ctx, user) datastore.PutUser(ctx, user)
} else if err != nil { } else if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)

View file

@ -163,6 +163,7 @@ func PostUserSync(c web.C, w http.ResponseWriter, r *http.Request) {
if user_token != nil { if user_token != nil {
user.Access = user_token.AccessToken user.Access = user_token.AccessToken
user.Secret = user_token.RefreshToken user.Secret = user_token.RefreshToken
user.TokenExpiry = user_token.Expiry
} else if err != nil { } else if err != nil {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
return return

View file

@ -9,4 +9,5 @@ type Login struct {
Secret string Secret string
Name string Name string
Email string Email string
Expiry int64
} }

View file

@ -1,11 +1,7 @@
package model package model
import (
"time"
)
type Token struct { type Token struct {
AccessToken string AccessToken string
RefreshToken string RefreshToken string
Expiry time.Time Expiry int64
} }

View file

@ -20,6 +20,7 @@ type User struct {
Created int64 `meddler:"user_created" json:"created_at"` Created int64 `meddler:"user_created" json:"created_at"`
Updated int64 `meddler:"user_updated" json:"updated_at"` Updated int64 `meddler:"user_updated" json:"updated_at"`
Synced int64 `meddler:"user_synced" json:"synced_at"` Synced int64 `meddler:"user_synced" json:"synced_at"`
TokenExpiry int64 `meddler:"user_access_expires" json:"-"`
} }
func NewUser(remote, login, email string) *User { func NewUser(remote, login, email string) *User {