casdoor/object/token.go
2021-03-14 00:18:11 +08:00

122 lines
2.9 KiB
Go

// Copyright 2021 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package object
import (
"github.com/casdoor/casdoor/util"
"xorm.io/core"
)
type Token struct {
Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
Name string `xorm:"varchar(100) notnull pk" json:"name"`
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
Application string `xorm:"varchar(100)" json:"application"`
AccessToken string `xorm:"varchar(100)" json:"accessToken"`
ExpiresIn int `json:"expiresIn"`
Scope string `xorm:"varchar(100)" json:"scope"`
TokenType string `xorm:"varchar(100)" json:"tokenType"`
}
func GetTokens(owner string) []*Token {
tokens := []*Token{}
err := adapter.engine.Desc("created_time").Find(&tokens, &Token{Owner: owner})
if err != nil {
panic(err)
}
return tokens
}
func getToken(owner string, name string) *Token {
token := Token{Owner: owner, Name: name}
existed, err := adapter.engine.Get(&token)
if err != nil {
panic(err)
}
if existed {
return &token
} else {
return nil
}
}
func GetToken(id string) *Token {
owner, name := util.GetOwnerAndNameFromId(id)
return getToken(owner, name)
}
func UpdateToken(id string, token *Token) bool {
owner, name := util.GetOwnerAndNameFromId(id)
if getToken(owner, name) == nil {
return false
}
_, err := adapter.engine.ID(core.PK{owner, name}).AllCols().Update(token)
if err != nil {
panic(err)
}
//return affected != 0
return true
}
func AddToken(token *Token) bool {
affected, err := adapter.engine.Insert(token)
if err != nil {
panic(err)
}
return affected != 0
}
func DeleteToken(token *Token) bool {
affected, err := adapter.engine.ID(core.PK{token.Owner, token.Name}).Delete(&Token{})
if err != nil {
panic(err)
}
return affected != 0
}
func GetOAuthToken(applicationId string, grantType string, clientId string, clientSecret string, scope string) *Token {
application := GetApplication(applicationId)
if grantType != "client_credentials" {
return nil
}
if application.ClientId != clientId || application.ClientSecret != clientSecret {
return nil
}
token := &Token{
Owner: application.Owner,
Name: util.GenerateId(),
CreatedTime: util.GetCurrentTime(),
Application: application.Name,
AccessToken: "",
ExpiresIn: 7200,
Scope: scope,
TokenType: "Bearer",
}
AddToken(token)
return token
}