summaryrefslogtreecommitdiffstats
path: root/models/oauth2_application.go
diff options
context:
space:
mode:
authorJonas Franz <info@jonasfranz.software>2019-04-17 10:18:16 +0200
committerLunny Xiao <xiaolunwen@gmail.com>2019-04-17 16:18:16 +0800
commit7a4c29c739fa9b08f901220ebcb2948daf491692 (patch)
tree73e44404a6c13061b832624582e230eedb801c3c /models/oauth2_application.go
parent34548369e1d78eb1141aecd4ab02acf59f2949ae (diff)
downloadgitea-7a4c29c739fa9b08f901220ebcb2948daf491692.tar.gz
gitea-7a4c29c739fa9b08f901220ebcb2948daf491692.zip
OAuth2 Grant UI (#6625)
* Add oauth2 grants ui Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add delete functionality Add translations Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Refactor DeleteOAuth2Grant Use results.Close() Signed-off-by: Jonas Franz <info@jonasfranz.software> * Refactor DeleteOAuth2Grant (again) Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if user ID is zero Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if grant ID is zero Signed-off-by: Jonas Franz <info@jonasfranz.software>
Diffstat (limited to 'models/oauth2_application.go')
-rw-r--r--models/oauth2_application.go55
1 files changed, 49 insertions, 6 deletions
diff --git a/models/oauth2_application.go b/models/oauth2_application.go
index dd80a79b48..1e69dd6430 100644
--- a/models/oauth2_application.go
+++ b/models/oauth2_application.go
@@ -340,12 +340,13 @@ func getOAuth2AuthorizationByCode(e Engine, code string) (auth *OAuth2Authorizat
// OAuth2Grant represents the permission of an user for a specifc application to access resources
type OAuth2Grant struct {
- ID int64 `xorm:"pk autoincr"`
- UserID int64 `xorm:"INDEX unique(user_application)"`
- ApplicationID int64 `xorm:"INDEX unique(user_application)"`
- Counter int64 `xorm:"NOT NULL DEFAULT 1"`
- CreatedUnix util.TimeStamp `xorm:"created"`
- UpdatedUnix util.TimeStamp `xorm:"updated"`
+ ID int64 `xorm:"pk autoincr"`
+ UserID int64 `xorm:"INDEX unique(user_application)"`
+ Application *OAuth2Application `xorm:"-"`
+ ApplicationID int64 `xorm:"INDEX unique(user_application)"`
+ Counter int64 `xorm:"NOT NULL DEFAULT 1"`
+ CreatedUnix util.TimeStamp `xorm:"created"`
+ UpdatedUnix util.TimeStamp `xorm:"updated"`
}
// TableName sets the table name to `oauth2_grant`
@@ -410,6 +411,48 @@ func getOAuth2GrantByID(e Engine, id int64) (grant *OAuth2Grant, err error) {
return
}
+// GetOAuth2GrantsByUserID lists all grants of a certain user
+func GetOAuth2GrantsByUserID(uid int64) ([]*OAuth2Grant, error) {
+ return getOAuth2GrantsByUserID(x, uid)
+}
+
+func getOAuth2GrantsByUserID(e Engine, uid int64) ([]*OAuth2Grant, error) {
+ type joinedOAuth2Grant struct {
+ Grant *OAuth2Grant `xorm:"extends"`
+ Application *OAuth2Application `xorm:"extends"`
+ }
+ var results *xorm.Rows
+ var err error
+ if results, err = e.
+ Table("oauth2_grant").
+ Where("user_id = ?", uid).
+ Join("INNER", "oauth2_application", "application_id = oauth2_application.id").
+ Rows(new(joinedOAuth2Grant)); err != nil {
+ return nil, err
+ }
+ defer results.Close()
+ grants := make([]*OAuth2Grant, 0)
+ for results.Next() {
+ joinedGrant := new(joinedOAuth2Grant)
+ if err := results.Scan(joinedGrant); err != nil {
+ return nil, err
+ }
+ joinedGrant.Grant.Application = joinedGrant.Application
+ grants = append(grants, joinedGrant.Grant)
+ }
+ return grants, nil
+}
+
+// RevokeOAuth2Grant deletes the grant with grantID and userID
+func RevokeOAuth2Grant(grantID, userID int64) error {
+ return revokeOAuth2Grant(x, grantID, userID)
+}
+
+func revokeOAuth2Grant(e Engine, grantID, userID int64) error {
+ _, err := e.Delete(&OAuth2Grant{ID: grantID, UserID: userID})
+ return err
+}
+
//////////////////////////////////////////////////////////////
// OAuth2TokenType represents the type of token for an oauth application