mirror of
https://github.com/netbirdio/dex.git
synced 2026-05-22 18:43:53 -07:00
Signed-off-by: Bob Maertz <1771054+bobmaertz@users.noreply.github.com>
37 lines
1.2 KiB
Go
37 lines
1.2 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/dexidp/dex/storage"
|
|
"github.com/dexidp/dex/storage/ent/db/devicerequest"
|
|
)
|
|
|
|
// CreateDeviceRequest saves provided device request into the database.
|
|
func (d *Database) CreateDeviceRequest(ctx context.Context, request storage.DeviceRequest) error {
|
|
_, err := d.client.DeviceRequest.Create().
|
|
SetClientID(request.ClientID).
|
|
SetClientSecret(request.ClientSecret).
|
|
SetScopes(request.Scopes).
|
|
SetUserCode(request.UserCode).
|
|
SetDeviceCode(request.DeviceCode).
|
|
// Save utc time into database because ent doesn't support comparing dates with different timezones
|
|
SetExpiry(request.Expiry.UTC()).
|
|
Save(ctx)
|
|
if err != nil {
|
|
return convertDBError("create device request: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetDeviceRequest extracts a device request from the database by user code.
|
|
func (d *Database) GetDeviceRequest(ctx context.Context, userCode string) (storage.DeviceRequest, error) {
|
|
deviceRequest, err := d.client.DeviceRequest.Query().
|
|
Where(devicerequest.UserCode(userCode)).
|
|
Only(ctx)
|
|
if err != nil {
|
|
return storage.DeviceRequest{}, convertDBError("get device request: %w", err)
|
|
}
|
|
return toStorageDeviceRequest(deviceRequest), nil
|
|
}
|