-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
Add OIDC #374
Merged
Merged
Add OIDC #374
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7b38244
build: add oauth2
trueChazza 60df3cd
feat: add initial flow
trueChazza 6e089f4
feat: add oauth authentik
trueChazza 00eba48
refactor: footer v3.2.0-beta.2
trueChazza eb2a50a
refactor: refactor generic oauth
trueChazza 90ca649
Merge branch 'master' into feature/add-oidc
trueChazza 0ad0f47
refactor: refactor
trueChazza 5d8f0fa
test: add mock server
trueChazza e002d39
refactor: refactor
trueChazza 3ed8afa
test: add oauth_controller_test
trueChazza 1eef965
build: remove authentik docker compose
trueChazza 0397ba6
test: refactor oauth_controller_test
trueChazza 1264bf3
Merge branch 'master' into feature/add-oidc
trueChazza f56b4ed
build: remove .env gitignore
trueChazza fe1c745
refactor: refactor
trueChazza a7705d4
ci: refactor workflows
trueChazza 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 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 |
---|---|---|
|
@@ -7,6 +7,7 @@ on: | |
|
||
jobs: | ||
docker: | ||
name: beta | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Set env | ||
|
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,96 @@ | ||
name: Test | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
branches: | ||
- '**' | ||
|
||
env: | ||
MIX_ENV: test | ||
APP_URL: http://localhost:4000 | ||
|
||
DB_HOSTNAME_TEST: localhost | ||
DB_USERNAME: my_user | ||
DB_PASSWORD: my_password | ||
DB_DATABASE: my_database | ||
|
||
OAUTH_CLIENT_ID: someClientId | ||
OAUTH_CLIENT_SECRET: someClientSecret | ||
OAUTH_ISSUER_URL: http://localhost:8081 | ||
OAUTH_AUTHORIZE_URL: http://localhost:8081/authorize | ||
OAUTH_TOKEN_URL: http://localhost:8081/token | ||
OAUTH_REDIRECT_URI: http://localhost:8081/auth/callback | ||
OAUTH_USER_URL: http://localhost:8081/user | ||
|
||
RADARR_BASE_URL: http://localhost:7878 | ||
RADARR_API_KEY: d031e8c9b9df4b2fab311d1c3b3fa2c5 | ||
SONARR_BASE_URL: http://localhost:8989 | ||
SONARR_API_KEY: 1accda4476394bfcaddefe8c4fd77d4a | ||
|
||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} | ||
|
||
jobs: | ||
|
||
test: | ||
name: test | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
otp: [ '24' ] | ||
elixir: [ '1.14.3' ] | ||
|
||
services: | ||
postgres-test: | ||
image: postgres | ||
env: | ||
POSTGRES_USER: my_user | ||
POSTGRES_PASSWORD: my_password | ||
POSTGRES_DB: my_database | ||
options: >- | ||
--health-cmd pg_isready | ||
--health-interval 10s | ||
--health-timeout 5s | ||
--health-retries 5 | ||
ports: | ||
- 5432:5432 | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: FedericoCarboni/setup-ffmpeg@v2 | ||
|
||
# Setup library | ||
- run: sudo cp -r ${GITHUB_WORKSPACE}/dev/library /library | ||
|
||
# Setup Radarr | ||
- run: chmod u+x .github/install-radarr.sh | ||
- run: ./.github/install-radarr.sh | ||
- run: curl --head -X GET --retry 5 --retry-connrefused --retry-delay 5 "http://localhost:7878" | ||
|
||
# Seed Radarr | ||
- run: curl -d "@${GITHUB_WORKSPACE}/dev/radarr/root-folder.json" -H "Content-Type:application/json" -X POST "http://localhost:7878/api/v3/rootfolder?apiKey=d031e8c9b9df4b2fab311d1c3b3fa2c5" | ||
- run: curl -d "@${GITHUB_WORKSPACE}/dev/radarr/movies.json" -H "Content-Type:application/json" -X POST "http://localhost:7878/api/v3/movie/import?apiKey=d031e8c9b9df4b2fab311d1c3b3fa2c5" | ||
|
||
# Setup Sonarr | ||
- run: chmod u+x .github/install-sonarr.sh | ||
- run: ./.github/install-sonarr.sh | ||
- run: curl --head -X GET --retry 5 --retry-connrefused --retry-delay 5 "http://localhost:8989" | ||
|
||
# Seed Sonarr | ||
- run: curl -d "@${GITHUB_WORKSPACE}/dev/sonarr/root-folder.json" -H "Content-Type:application/json" -X POST "http://localhost:8989/api/v3/rootfolder?apikey=1accda4476394bfcaddefe8c4fd77d4a" | ||
- run: curl -d "@${GITHUB_WORKSPACE}/dev/sonarr/series.json" -H "Content-Type:application/json" -X POST "http://localhost:8989/api/v3/series/import?apikey=1accda4476394bfcaddefe8c4fd77d4a" | ||
|
||
# Run tests | ||
- uses: erlef/setup-beam@v1 | ||
with: | ||
otp-version: ${{matrix.otp}} | ||
elixir-version: ${{matrix.elixir}} | ||
- run: mix deps.get | ||
- run: mix ecto.migrate | ||
- run: mix run priv/repo/seeds.exs | ||
- run: mix test | ||
- run: chmod u+x code-coverage.sh | ||
- run: ./code-coverage.sh |
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,20 @@ | ||
defmodule MediaServerWeb.OAuthController do | ||
use MediaServerWeb, :controller | ||
|
||
def index(conn, _params) do | ||
conn | ||
|> redirect(external: MediaServerWeb.OAuth.authorize_url!) | ||
end | ||
|
||
def callback(conn, %{"code" => code}) do | ||
client = MediaServerWeb.OAuth.get_token!(code: code) | ||
|
||
if user = MediaServer.Accounts.get_user_by_email(MediaServerWeb.OAuth.get_user!(client).email) do | ||
|
||
MediaServerWeb.UserAuth.log_in_user(conn, user) | ||
end | ||
|
||
conn | ||
|> redirect(to: "/") | ||
end | ||
end |
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,51 @@ | ||
defmodule MediaServerWeb.OAuth do | ||
use OAuth2.Strategy | ||
|
||
alias OAuth2.Strategy.AuthCode | ||
|
||
defp config do | ||
[ | ||
strategy: __MODULE__, | ||
client_id: System.get_env("OAUTH_CLIENT_ID"), | ||
client_secret: System.get_env("OAUTH_CLIENT_SECRET"), | ||
site: System.get_env("OAUTH_ISSUER_URL"), | ||
authorize_url: System.get_env("OAUTH_AUTHORIZE_URL"), | ||
token_url: System.get_env("OAUTH_TOKEN_URL"), | ||
redirect_uri: System.get_env("OAUTH_REDIRECT_URI") | ||
] | ||
end | ||
|
||
def client do | ||
OAuth2.Client.new(config()) | ||
end | ||
|
||
def authorize_url!(_params \\ []) do | ||
OAuth2.Client.authorize_url!(client()) | ||
end | ||
|
||
def authorize_url(client, params) do | ||
AuthCode.authorize_url(client, params) | ||
end | ||
|
||
def get_token!(params \\ [], _headers \\ []) do | ||
OAuth2.Client.get_token!(client(), params) | ||
end | ||
|
||
def get_token(client, params, headers) do | ||
client | ||
|> put_header("Accept", "application/json") | ||
|> AuthCode.get_token(params, headers) | ||
end | ||
|
||
def get_user!(client) do | ||
token = Map.get(client, :token) |> Map.get(:access_token) |> Jason.decode! |> Map.get("access_token") | ||
|
||
%{body: user} = OAuth2.Client.get!(client, System.get_env("OAUTH_USER_URL"), [ | ||
{"authorization", "Bearer #{ token }"} | ||
]) | ||
|
||
decoded_user = Jason.decode!(user) | ||
|
||
%{name: decoded_user["name"], email: decoded_user["email"]} | ||
end | ||
end |
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
20 changes: 20 additions & 0 deletions
20
test/media_server_web/controllers/oauth_controller_test.exs
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,20 @@ | ||
defmodule MediaServerWeb.OauthControllerTest do | ||
use MediaServerWeb.ConnCase | ||
|
||
test "it should redirect", %{conn: conn} do | ||
conn = get(conn, Routes.o_auth_path(conn, :index)) | ||
response = html_response(conn, 302) | ||
|
||
assert response =~ "You are being" | ||
assert response =~ "redirected" | ||
assert response =~ "authorize?client_id=" | ||
end | ||
|
||
test "it should callback", %{conn: conn} do | ||
conn = get(conn, Routes.o_auth_path(conn, :callback, code: "code")) | ||
response = html_response(conn, 302) | ||
|
||
assert response =~ "You are being" | ||
assert response =~ "redirected" | ||
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.
@onedr0p Example environment variables for configuring oauth.
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.
Looks good so far.