-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(auth): add Entra ID OIDC authentication for web UI #6345
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
Open
rbstp
wants to merge
1
commit into
runatlantis:main
Choose a base branch
from
rbstp:feat/entraid
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,112 @@ | ||
| // Copyright 2025 The Atlantis Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package controllers | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/runatlantis/atlantis/server/logging" | ||
| "github.com/runatlantis/atlantis/server/oidc" | ||
| ) | ||
|
|
||
| // OIDCController handles the OIDC authentication flow for the Atlantis web UI. | ||
| type OIDCController struct { | ||
| Provider *oidc.Provider | ||
| SessionManager *oidc.SessionManager | ||
| Logger logging.SimpleLogging | ||
| BasePath string | ||
| } | ||
|
|
||
| func (o *OIDCController) homeURL() string { | ||
| if o.BasePath == "" || o.BasePath == "/" { | ||
| return "/" | ||
| } | ||
| return o.BasePath + "/" | ||
| } | ||
|
|
||
| // Login initiates the OIDC authorization code flow by redirecting the user | ||
| // to the identity provider's authorization endpoint. | ||
| func (o *OIDCController) Login(w http.ResponseWriter, r *http.Request) { | ||
| state, err := o.SessionManager.CreateState(w) | ||
| if err != nil { | ||
| o.Logger.Err("creating OIDC state - %s", err) | ||
| http.Error(w, "Internal server error", http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| authURL := o.Provider.AuthCodeURL(state) | ||
| o.Logger.Debug("OIDC login - redirecting to %q", authURL) | ||
| http.Redirect(w, r, authURL, http.StatusFound) | ||
| } | ||
|
|
||
| // Callback handles the OIDC callback from the identity provider after user | ||
| // authentication. It exchanges the authorization code for tokens, verifies | ||
| // the ID token, and establishes a session cookie. | ||
| func (o *OIDCController) Callback(w http.ResponseWriter, r *http.Request) { | ||
| // Check for errors from the IDP. | ||
| if errParam := r.URL.Query().Get("error"); errParam != "" { | ||
| errDesc := r.URL.Query().Get("error_description") | ||
| o.Logger.Err("OIDC callback error from IDP - %s - %s", errParam, errDesc) | ||
| http.Error(w, fmt.Sprintf("Authentication error: %s - %s", errParam, errDesc), http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| // Verify state parameter. | ||
| state := r.URL.Query().Get("state") | ||
| if state == "" { | ||
| http.Error(w, "Missing state parameter", http.StatusBadRequest) | ||
| return | ||
| } | ||
| if err := o.SessionManager.VerifyState(r, state); err != nil { | ||
| o.Logger.Err("verifying OIDC state - %s", err) | ||
| http.Error(w, "Invalid state parameter", http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| // Exchange authorization code for tokens. | ||
| code := r.URL.Query().Get("code") | ||
| if code == "" { | ||
| http.Error(w, "Missing authorization code", http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| _, rawIDToken, err := o.Provider.Exchange(r.Context(), code) | ||
| if err != nil { | ||
| o.Logger.Err("exchanging OIDC token - %s", err) | ||
| http.Error(w, "Authentication failed", http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| // Extract the email (or preferred_username) from the ID token to store | ||
| // in the session cookie. This keeps the cookie small instead of | ||
| // embedding the full raw ID token. | ||
| email := oidc.ExtractEmail(rawIDToken) | ||
| if email == "" { | ||
| o.Logger.Err("OIDC callback - no email or preferred_username in ID token") | ||
| http.Error(w, "Authentication failed: no user identity in token", http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| // Set session cookie with only the email claim. | ||
| if err := o.SessionManager.SetSession(w, email); err != nil { | ||
| o.Logger.Err("setting OIDC session - %s", err) | ||
| http.Error(w, "Internal server error", http.StatusInternalServerError) | ||
| return | ||
| } | ||
|
|
||
| // Clear the state cookie. | ||
| o.SessionManager.ClearState(w) | ||
|
|
||
| o.Logger.Info("OIDC login successful, redirecting to %q", o.homeURL()) | ||
| http.Redirect(w, r, o.homeURL(), http.StatusFound) | ||
| } | ||
|
|
||
| // Logout clears the OIDC session cookie and redirects to the home page. | ||
| func (o *OIDCController) Logout(w http.ResponseWriter, r *http.Request) { | ||
| o.SessionManager.ClearSession(w) | ||
|
|
||
| o.Logger.Info("OIDC logout, redirecting to %q", o.homeURL()) | ||
| http.Redirect(w, r, o.homeURL(), http.StatusFound) | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.