Skip to content

Instantly share code, notes, and snippets.

@guotie
Last active September 11, 2016 00:39
Show Gist options
  • Save guotie/725d56f9972d98749493fbd0e2bebc8c to your computer and use it in GitHub Desktop.
Save guotie/725d56f9972d98749493fbd0e2bebc8c to your computer and use it in GitHub Desktop.
oauth2 client
package user
import (
"fmt"
"time"
//"dxmall/utils"
"github.com/jinzhu/gorm"
)
// 可以发起 oauth2 认证的客户端
type DxClient struct {
Id int
Name string `gorm:"NOT NULL;unique;index;size:60" json:"name"`
Secret string `gorm:"NOT NULL;size:120" json:"secret"`
IsDel bool `gorm:"NOT NULL"`
AccessSeconds int `gorm:"NOT NULL;DEFAULT:86400" json:"access_seconds"`
RefreshSeconds int `gorm:"NOT NULL;DEFAULT:0" json:"refresh_seconds"` // 0代表没有refresh token
CreatedAt time.Time
}
//
func AuthenClient(db *gorm.DB, name, secret string) (*DxClient, error) {
var (
err error
client DxClient
)
err = db.Where("name=? And is_del=0", name).First(&client).Error
if err != nil {
return nil, err
}
if client.Secret != secret {
return nil, fmt.Errorf("secret should equal")
}
//err = utils.VerifyPassword(client.Secret, secret)
return &client, err
}
func CreateClient(db *gorm.DB, name, secret string, as, rs int) error {
var client DxClient
client.Name = name
client.Secret = secret //utils.HashPassword(secret)
client.IsDel = false
client.AccessSeconds = as
client.RefreshSeconds = rs
return db.Create(&client).Error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment