-
Notifications
You must be signed in to change notification settings - Fork 56
feat: token exchange as part of VSC login #1240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5b96bed
create auth flow for token exchange
heyzec c90d7d3
clean up create_user_and_tokens
heyzec 45da083
update example env file
heyzec 3ab518a
schedule deletion job
heyzec 20cf57d
rename CodeExchange to TokenExchange
heyzec fd1f9af
fix formatting
heyzec 331b25f
fix credo issues
heyzec 8a8b0d8
Merge branch 'master' into vscode/auth
RichDom2185 736191c
Merge branch 'master' into vscode/auth
RichDom2185 600b290
make "code" PK in token_exchange table
heyzec f12fa61
Merge branch 'master' into vscode/auth
RichDom2185 495aa50
Rename migration file to maintain total order
RichDom2185 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,60 @@ | ||
defmodule Cadet.TokenExchange do | ||
@moduledoc """ | ||
The TokenExchange entity stores short-lived codes to be exchanged for long-lived auth tokens. | ||
""" | ||
use Cadet, :model | ||
|
||
import Ecto.Query | ||
|
||
alias Cadet.Repo | ||
alias Cadet.Accounts.User | ||
|
||
@primary_key {:code, :string, []} | ||
schema "token_exchange" do | ||
field(:generated_at, :utc_datetime_usec) | ||
field(:expires_at, :utc_datetime_usec) | ||
|
||
belongs_to(:user, User) | ||
|
||
timestamps() | ||
end | ||
|
||
@required_fields ~w(code generated_at expires_at user_id)a | ||
|
||
def get_by_code(code) do | ||
case Repo.get_by(__MODULE__, code: code) do | ||
nil -> | ||
{:error, "Not found"} | ||
|
||
struct -> | ||
if Timex.before?(struct.expires_at, Timex.now()) do | ||
{:error, "Expired"} | ||
else | ||
struct = Repo.preload(struct, :user) | ||
Repo.delete(struct) | ||
{:ok, struct} | ||
end | ||
end | ||
end | ||
|
||
def delete_expired do | ||
now = Timex.now() | ||
|
||
Repo.delete_all(from(c in __MODULE__, where: c.expires_at < ^now)) | ||
end | ||
|
||
def changeset(struct, attrs) do | ||
struct | ||
|> cast(attrs, @required_fields) | ||
|> validate_required(@required_fields) | ||
end | ||
|
||
def insert(attrs) do | ||
changeset = | ||
%__MODULE__{} | ||
|> changeset(attrs) | ||
|
||
changeset | ||
|> Repo.insert() | ||
end | ||
end |
This file contains hidden or 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 hidden or 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
13 changes: 13 additions & 0 deletions
13
priv/repo/migrations/20250417093922_create_token_exchange_table.exs
This file contains hidden or 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,13 @@ | ||
defmodule Cadet.Repo.Migrations.CreateTokenExchangeTable do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:token_exchange) do | ||
add(:code, :string, null: false) | ||
add(:generated_at, :utc_datetime_usec, null: false) | ||
add(:expires_at, :utc_datetime_usec, null: false) | ||
add(:user_id, references(:users), null: false) | ||
timestamps() | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.env changes