forked from fabric8-services/fabric8-wit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
122 lines (106 loc) · 3.69 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package auth
import (
"github.com/fabric8io/almighty-core/convert"
"github.com/fabric8io/almighty-core/errors"
"github.com/fabric8io/almighty-core/gormsupport"
"github.com/fabric8io/almighty-core/log"
"context"
"github.com/jinzhu/gorm"
uuid "github.com/satori/go.uuid"
)
const (
oauthStateTableName = "oauth_state_references"
)
// OauthStateReference represents a oauth state reference
type OauthStateReference struct {
gormsupport.Lifecycle
ID uuid.UUID `sql:"type:uuid default uuid_generate_v4()" gorm:"primary_key"`
Referrer string
}
// TableName implements gorm.tabler
func (r OauthStateReference) TableName() string {
return oauthStateTableName
}
// Equal returns true if two States objects are equal; otherwise false is returned.
func (r OauthStateReference) Equal(u convert.Equaler) bool {
other, ok := u.(OauthStateReference)
if !ok {
return false
}
if r.ID != other.ID {
return false
}
if r.Referrer != other.Referrer {
return false
}
return true
}
// OauthStateReferenceRepository encapsulate storage & retrieval of state references
type OauthStateReferenceRepository interface {
Create(ctx context.Context, state *OauthStateReference) (*OauthStateReference, error)
Delete(ctx context.Context, ID uuid.UUID) error
Load(ctx context.Context, ID uuid.UUID) (*OauthStateReference, error)
}
// NewOauthStateReferenceRepository creates a new oauth state reference repo
func NewOauthStateReferenceRepository(db *gorm.DB) *GormOauthStateReferenceRepository {
return &GormOauthStateReferenceRepository{db}
}
// GormOauthStateReferenceRepository implements OauthStateReferenceRepository using gorm
type GormOauthStateReferenceRepository struct {
db *gorm.DB
}
// Delete deletes the reference with the given id
// returns NotFoundError or InternalError
func (r *GormOauthStateReferenceRepository) Delete(ctx context.Context, ID uuid.UUID) error {
if ID == uuid.Nil {
log.Error(ctx, map[string]interface{}{
"oauth_state_reference_id": ID.String(),
}, "unable to find the oauth state reference by ID")
return errors.NewNotFoundError("oauth state reference", ID.String())
}
reference := OauthStateReference{ID: ID}
tx := r.db.Delete(reference)
if err := tx.Error; err != nil {
log.Error(ctx, map[string]interface{}{
"oauth_state_reference_id": ID.String(),
}, "unable to delete the oauth state reference")
return errors.NewInternalError(err.Error())
}
if tx.RowsAffected == 0 {
log.Error(ctx, map[string]interface{}{
"oauth state reference": ID.String(),
}, "none row was affected by the deletion operation")
return errors.NewNotFoundError("oauth state reference", ID.String())
}
return nil
}
// Create creates a new oauth state reference in the DB
// returns InternalError
func (r *GormOauthStateReferenceRepository) Create(ctx context.Context, reference *OauthStateReference) (*OauthStateReference, error) {
if reference.ID == uuid.Nil {
reference.ID = uuid.NewV4()
}
tx := r.db.Create(reference)
if err := tx.Error; err != nil {
return nil, errors.NewInternalError(err.Error())
}
log.Info(ctx, map[string]interface{}{
"oauth_state_reference_id": reference.ID,
}, "Oauth state reference created successfully")
return reference, nil
}
// Load loads state reference by ID
func (r *GormOauthStateReferenceRepository) Load(ctx context.Context, id uuid.UUID) (*OauthStateReference, error) {
ref := OauthStateReference{}
tx := r.db.Where("id=?", id).First(&ref)
if tx.RecordNotFound() {
log.Error(ctx, map[string]interface{}{
"id": id.String(),
}, "Could not find oauth state reference by state")
return nil, errors.NewNotFoundError("oauth state reference", id.String())
}
if tx.Error != nil {
return nil, errors.NewInternalError(tx.Error.Error())
}
return &ref, nil
}