-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1906 from flant/ent-sqlite
feat: Add ent-based sqlite3 storage
- Loading branch information
Showing
114 changed files
with
41,894 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/dexidp/dex/storage" | ||
) | ||
|
||
// CreateAuthCode saves provided auth code into the database. | ||
func (d *Database) CreateAuthCode(code storage.AuthCode) error { | ||
_, err := d.client.AuthCode.Create(). | ||
SetID(code.ID). | ||
SetClientID(code.ClientID). | ||
SetScopes(code.Scopes). | ||
SetRedirectURI(code.RedirectURI). | ||
SetNonce(code.Nonce). | ||
SetClaimsUserID(code.Claims.UserID). | ||
SetClaimsEmail(code.Claims.Email). | ||
SetClaimsEmailVerified(code.Claims.EmailVerified). | ||
SetClaimsUsername(code.Claims.Username). | ||
SetClaimsPreferredUsername(code.Claims.PreferredUsername). | ||
SetClaimsGroups(code.Claims.Groups). | ||
SetCodeChallenge(code.PKCE.CodeChallenge). | ||
SetCodeChallengeMethod(code.PKCE.CodeChallengeMethod). | ||
// Save utc time into database because ent doesn't support comparing dates with different timezones | ||
SetExpiry(code.Expiry.UTC()). | ||
SetConnectorID(code.ConnectorID). | ||
SetConnectorData(code.ConnectorData). | ||
Save(context.TODO()) | ||
if err != nil { | ||
return convertDBError("create auth code: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
// GetAuthCode extracts an auth code from the database by id. | ||
func (d *Database) GetAuthCode(id string) (storage.AuthCode, error) { | ||
authCode, err := d.client.AuthCode.Get(context.TODO(), id) | ||
if err != nil { | ||
return storage.AuthCode{}, convertDBError("get auth code: %w", err) | ||
} | ||
return toStorageAuthCode(authCode), nil | ||
} | ||
|
||
// DeleteAuthCode deletes an auth code from the database by id. | ||
func (d *Database) DeleteAuthCode(id string) error { | ||
err := d.client.AuthCode.DeleteOneID(id).Exec(context.TODO()) | ||
if err != nil { | ||
return convertDBError("delete auth code: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/dexidp/dex/storage" | ||
) | ||
|
||
// CreateAuthRequest saves provided auth request into the database. | ||
func (d *Database) CreateAuthRequest(authRequest storage.AuthRequest) error { | ||
_, err := d.client.AuthRequest.Create(). | ||
SetID(authRequest.ID). | ||
SetClientID(authRequest.ClientID). | ||
SetScopes(authRequest.Scopes). | ||
SetResponseTypes(authRequest.ResponseTypes). | ||
SetRedirectURI(authRequest.RedirectURI). | ||
SetState(authRequest.State). | ||
SetNonce(authRequest.Nonce). | ||
SetForceApprovalPrompt(authRequest.ForceApprovalPrompt). | ||
SetLoggedIn(authRequest.LoggedIn). | ||
SetClaimsUserID(authRequest.Claims.UserID). | ||
SetClaimsEmail(authRequest.Claims.Email). | ||
SetClaimsEmailVerified(authRequest.Claims.EmailVerified). | ||
SetClaimsUsername(authRequest.Claims.Username). | ||
SetClaimsPreferredUsername(authRequest.Claims.PreferredUsername). | ||
SetClaimsGroups(authRequest.Claims.Groups). | ||
SetCodeChallenge(authRequest.PKCE.CodeChallenge). | ||
SetCodeChallengeMethod(authRequest.PKCE.CodeChallengeMethod). | ||
// Save utc time into database because ent doesn't support comparing dates with different timezones | ||
SetExpiry(authRequest.Expiry.UTC()). | ||
SetConnectorID(authRequest.ConnectorID). | ||
SetConnectorData(authRequest.ConnectorData). | ||
Save(context.TODO()) | ||
if err != nil { | ||
return convertDBError("create auth request: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
// GetAuthRequest extracts an auth request from the database by id. | ||
func (d *Database) GetAuthRequest(id string) (storage.AuthRequest, error) { | ||
authRequest, err := d.client.AuthRequest.Get(context.TODO(), id) | ||
if err != nil { | ||
return storage.AuthRequest{}, convertDBError("get auth request: %w", err) | ||
} | ||
return toStorageAuthRequest(authRequest), nil | ||
} | ||
|
||
// DeleteAuthRequest deletes an auth request from the database by id. | ||
func (d *Database) DeleteAuthRequest(id string) error { | ||
err := d.client.AuthRequest.DeleteOneID(id).Exec(context.TODO()) | ||
if err != nil { | ||
return convertDBError("delete auth request: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
// UpdateAuthRequest changes an auth request by id using an updater function and saves it to the database. | ||
func (d *Database) UpdateAuthRequest(id string, updater func(old storage.AuthRequest) (storage.AuthRequest, error)) error { | ||
tx, err := d.client.Tx(context.TODO()) | ||
if err != nil { | ||
return fmt.Errorf("update auth request tx: %w", err) | ||
} | ||
|
||
authRequest, err := tx.AuthRequest.Get(context.TODO(), id) | ||
if err != nil { | ||
return rollback(tx, "update auth request database: %w", err) | ||
} | ||
|
||
newAuthRequest, err := updater(toStorageAuthRequest(authRequest)) | ||
if err != nil { | ||
return rollback(tx, "update auth request updating: %w", err) | ||
} | ||
|
||
_, err = tx.AuthRequest.UpdateOneID(newAuthRequest.ID). | ||
SetClientID(newAuthRequest.ClientID). | ||
SetScopes(newAuthRequest.Scopes). | ||
SetResponseTypes(newAuthRequest.ResponseTypes). | ||
SetRedirectURI(newAuthRequest.RedirectURI). | ||
SetState(newAuthRequest.State). | ||
SetNonce(newAuthRequest.Nonce). | ||
SetForceApprovalPrompt(newAuthRequest.ForceApprovalPrompt). | ||
SetLoggedIn(newAuthRequest.LoggedIn). | ||
SetClaimsUserID(newAuthRequest.Claims.UserID). | ||
SetClaimsEmail(newAuthRequest.Claims.Email). | ||
SetClaimsEmailVerified(newAuthRequest.Claims.EmailVerified). | ||
SetClaimsUsername(newAuthRequest.Claims.Username). | ||
SetClaimsPreferredUsername(newAuthRequest.Claims.PreferredUsername). | ||
SetClaimsGroups(newAuthRequest.Claims.Groups). | ||
SetCodeChallenge(newAuthRequest.PKCE.CodeChallenge). | ||
SetCodeChallengeMethod(newAuthRequest.PKCE.CodeChallengeMethod). | ||
// Save utc time into database because ent doesn't support comparing dates with different timezones | ||
SetExpiry(newAuthRequest.Expiry.UTC()). | ||
SetConnectorID(newAuthRequest.ConnectorID). | ||
SetConnectorData(newAuthRequest.ConnectorData). | ||
Save(context.TODO()) | ||
if err != nil { | ||
return rollback(tx, "update auth request uploading: %w", err) | ||
} | ||
|
||
if err = tx.Commit(); err != nil { | ||
return rollback(tx, "update auth request commit: %w", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.