Generates/Stores the device request and returns the device and user codes.
Signed-off-by: justin-slowik <justin.slowik@thermofisher.com>
This commit is contained in:
committed by
justin-slowik
parent
11fc8568cb
commit
6d343e059b
@@ -22,6 +22,8 @@ const (
|
||||
offlineSessionPrefix = "offline_session/"
|
||||
connectorPrefix = "connector/"
|
||||
keysName = "openid-connect-keys"
|
||||
deviceRequestPrefix = "device_req/"
|
||||
deviceTokenPrefix = "device_token/"
|
||||
|
||||
// defaultStorageTimeout will be applied to all storage's operations.
|
||||
defaultStorageTimeout = 5 * time.Second
|
||||
@@ -72,6 +74,36 @@ func (c *conn) GarbageCollect(now time.Time) (result storage.GCResult, err error
|
||||
result.AuthCodes++
|
||||
}
|
||||
}
|
||||
|
||||
deviceRequests, err := c.listDeviceRequests(ctx)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
for _, deviceRequest := range deviceRequests {
|
||||
if now.After(deviceRequest.Expiry) {
|
||||
if err := c.deleteKey(ctx, keyID(deviceRequestPrefix, deviceRequest.UserCode)); err != nil {
|
||||
c.logger.Errorf("failed to delete device request %v", err)
|
||||
delErr = fmt.Errorf("failed to delete device request: %v", err)
|
||||
}
|
||||
result.DeviceRequests++
|
||||
}
|
||||
}
|
||||
|
||||
deviceTokens, err := c.listDeviceTokens(ctx)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
for _, deviceToken := range deviceTokens {
|
||||
if now.After(deviceToken.Expiry) {
|
||||
if err := c.deleteKey(ctx, keyID(deviceTokenPrefix, deviceToken.DeviceCode)); err != nil {
|
||||
c.logger.Errorf("failed to delete device token %v", err)
|
||||
delErr = fmt.Errorf("failed to delete device token: %v", err)
|
||||
}
|
||||
result.DeviceTokens++
|
||||
}
|
||||
}
|
||||
return result, delErr
|
||||
}
|
||||
|
||||
@@ -531,3 +563,45 @@ func keyEmail(prefix, email string) string { return prefix + strings.ToLower(ema
|
||||
func keySession(prefix, userID, connID string) string {
|
||||
return prefix + strings.ToLower(userID+"|"+connID)
|
||||
}
|
||||
|
||||
func (c *conn) CreateDeviceRequest(d storage.DeviceRequest) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultStorageTimeout)
|
||||
defer cancel()
|
||||
return c.txnCreate(ctx, keyID(deviceRequestPrefix, d.UserCode), fromStorageDeviceRequest(d))
|
||||
}
|
||||
|
||||
func (c *conn) listDeviceRequests(ctx context.Context) (requests []DeviceRequest, err error) {
|
||||
res, err := c.db.Get(ctx, deviceRequestPrefix, clientv3.WithPrefix())
|
||||
if err != nil {
|
||||
return requests, err
|
||||
}
|
||||
for _, v := range res.Kvs {
|
||||
var r DeviceRequest
|
||||
if err = json.Unmarshal(v.Value, &r); err != nil {
|
||||
return requests, err
|
||||
}
|
||||
requests = append(requests, r)
|
||||
}
|
||||
return requests, nil
|
||||
}
|
||||
|
||||
func (c *conn) CreateDeviceToken(t storage.DeviceToken) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), defaultStorageTimeout)
|
||||
defer cancel()
|
||||
return c.txnCreate(ctx, keyID(deviceRequestPrefix, t.DeviceCode), fromStorageDeviceToken(t))
|
||||
}
|
||||
|
||||
func (c *conn) listDeviceTokens(ctx context.Context) (deviceTokens []DeviceToken, err error) {
|
||||
res, err := c.db.Get(ctx, deviceTokenPrefix, clientv3.WithPrefix())
|
||||
if err != nil {
|
||||
return deviceTokens, err
|
||||
}
|
||||
for _, v := range res.Kvs {
|
||||
var dt DeviceToken
|
||||
if err = json.Unmarshal(v.Value, &dt); err != nil {
|
||||
return deviceTokens, err
|
||||
}
|
||||
deviceTokens = append(deviceTokens, dt)
|
||||
}
|
||||
return deviceTokens, nil
|
||||
}
|
||||
|
@@ -216,3 +216,41 @@ func toStorageOfflineSessions(o OfflineSessions) storage.OfflineSessions {
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// DeviceRequest is a mirrored struct from storage with JSON struct tags
|
||||
type DeviceRequest struct {
|
||||
UserCode string `json:"user_code"`
|
||||
DeviceCode string `json:"device_code"`
|
||||
ClientID string `json:"client_id"`
|
||||
Scopes []string `json:"scopes"`
|
||||
PkceVerifier string `json:"pkce_verifier"`
|
||||
Expiry time.Time `json:"expiry"`
|
||||
}
|
||||
|
||||
func fromStorageDeviceRequest(d storage.DeviceRequest) DeviceRequest {
|
||||
return DeviceRequest{
|
||||
UserCode: d.UserCode,
|
||||
DeviceCode: d.DeviceCode,
|
||||
ClientID: d.ClientID,
|
||||
Scopes: d.Scopes,
|
||||
PkceVerifier: d.PkceVerifier,
|
||||
Expiry: d.Expiry,
|
||||
}
|
||||
}
|
||||
|
||||
// DeviceToken is a mirrored struct from storage with JSON struct tags
|
||||
type DeviceToken struct {
|
||||
DeviceCode string `json:"device_code"`
|
||||
Status string `json:"status"`
|
||||
Token string `json:"token"`
|
||||
Expiry time.Time `json:"expiry"`
|
||||
}
|
||||
|
||||
func fromStorageDeviceToken(t storage.DeviceToken) DeviceToken {
|
||||
return DeviceToken{
|
||||
DeviceCode: t.DeviceCode,
|
||||
Status: t.Status,
|
||||
Token: t.Token,
|
||||
Expiry: t.Expiry,
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user