-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[client] Android - Reuse the profile's account for Android SSO logins #6988
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
pappz
wants to merge
2
commits into
main
Choose a base branch
from
fix/android-login-hint
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package android | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| log "github.com/sirupsen/logrus" | ||
|
|
||
| "github.com/netbirdio/netbird/client/internal/profilemanager" | ||
| "github.com/netbirdio/netbird/util" | ||
| ) | ||
|
|
||
| const ( | ||
| // Android-specific config filename (different from desktop default.json) | ||
| defaultConfigFilename = "netbird.cfg" | ||
| // Subdirectory for non-default profiles (must match Java Preferences.java) | ||
| profilesSubdir = "profiles" | ||
| // profileAccountSuffix names the file holding the profile's account email. | ||
| // Deliberately not ".state.json", which desktop uses for the same data: | ||
| // there the email and the engine's state manager live in different | ||
| // directories, but on Android both resolve under files/, so sharing the name | ||
| // would have the two overwrite each other — the state manager rewrites the | ||
| // whole file from its own keys (see statemanager.Manager.PersistState), and | ||
| // this package's writer does the same in reverse. | ||
| profileAccountSuffix = ".account.json" | ||
| ) | ||
|
|
||
| // profileAccountPathFor derives the account file path from a profile's config | ||
| // path: netbird.cfg -> netbird.account.json, <id>.json -> <id>.account.json. | ||
| // | ||
| // Deriving from the config path rather than resolving the active profile keeps | ||
| // the write on the profile the login actually ran for: Auth.login runs in a | ||
| // goroutine, so the active profile can change under a flow already in flight. | ||
| func profileAccountPathFor(configPath string) (string, error) { | ||
| if configPath == "" { | ||
| return "", fmt.Errorf("empty config path") | ||
| } | ||
|
|
||
| base := filepath.Base(configPath) | ||
| stem := strings.TrimSuffix(base, filepath.Ext(base)) | ||
| if stem == "" || stem == "." { | ||
| return "", fmt.Errorf("config path %q has no filename stem", configPath) | ||
| } | ||
|
|
||
| return filepath.Join(filepath.Dir(configPath), stem+profileAccountSuffix), nil | ||
| } | ||
|
|
||
| // readProfileEmail returns the account email stored for the profile whose config | ||
| // lives at configPath. A missing or unreadable file yields "", which leaves the | ||
| // account choice to the IdP. | ||
| func readProfileEmail(configPath string) string { | ||
| accountPath, err := profileAccountPathFor(configPath) | ||
| if err != nil { | ||
| log.Debugf("no profile account path for login hint: %v", err) | ||
| return "" | ||
| } | ||
|
|
||
| var state profilemanager.ProfileState | ||
| if _, err := util.ReadJson(accountPath, &state); err != nil { | ||
| if !os.IsNotExist(err) { | ||
| log.Debugf("failed to read profile account for login hint: %v", err) | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| return state.Email | ||
| } | ||
|
|
||
| // writeProfileEmail records the account email for the profile whose config lives | ||
| // at configPath, so later logins can pass it as an OIDC login_hint. An empty | ||
| // email is ignored rather than blanking what is already stored. | ||
| func writeProfileEmail(configPath string, email string) error { | ||
| if email == "" { | ||
| return nil | ||
| } | ||
|
|
||
| accountPath, err := profileAccountPathFor(configPath) | ||
| if err != nil { | ||
| return fmt.Errorf("resolve profile account path: %w", err) | ||
| } | ||
|
|
||
| state := profilemanager.ProfileState{Email: email} | ||
| if err := util.WriteJsonWithRestrictedPermission(context.Background(), accountPath, state); err != nil { | ||
| return fmt.Errorf("write profile account: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // removeProfileEmail drops the stored account email. Called on logout: while the | ||
| // email is on disk it goes out as a login_hint, which would steer the next login | ||
| // straight back into the account just logged out of. Mirrors the desktop UI's | ||
| // RemoveProfileState call. | ||
| func removeProfileEmail(configPath string) error { | ||
| accountPath, err := profileAccountPathFor(configPath) | ||
| if err != nil { | ||
| return fmt.Errorf("resolve profile account path: %w", err) | ||
| } | ||
|
|
||
| if err := os.Remove(accountPath); err != nil && !os.IsNotExist(err) { | ||
| return fmt.Errorf("remove profile account: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } |
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.