-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup code, redirect to auth page when need
- Loading branch information
yusing
committed
Jan 12, 2025
1 parent
ef277ef
commit 76fe534
Showing
11 changed files
with
113 additions
and
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package auth | ||
|
||
const ( | ||
CookieToken = "token" | ||
CookieOauthState = "oauth_state" | ||
) |
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,45 @@ | ||
package auth | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
|
||
U "github.com/yusing/go-proxy/internal/api/v1/utils" | ||
"github.com/yusing/go-proxy/internal/common" | ||
E "github.com/yusing/go-proxy/internal/error" | ||
) | ||
|
||
var ( | ||
ErrInvalidUsername = E.New("invalid username") | ||
ErrInvalidPassword = E.New("invalid password") | ||
) | ||
|
||
func validatePassword(cred *Credentials) error { | ||
if cred.Username != common.APIUser { | ||
return ErrInvalidUsername.Subject(cred.Username) | ||
} | ||
if !bytes.Equal(common.HashPassword(cred.Password), common.APIPasswordHash) { | ||
return ErrInvalidPassword.Subject(cred.Password) | ||
} | ||
return nil | ||
} | ||
|
||
// UserPassLoginHandler handles user login. | ||
func UserPassLoginHandler(w http.ResponseWriter, r *http.Request) { | ||
var creds Credentials | ||
err := json.NewDecoder(r.Body).Decode(&creds) | ||
if err != nil { | ||
U.HandleErr(w, r, err, http.StatusBadRequest) | ||
return | ||
} | ||
if err := validatePassword(&creds); err != nil { | ||
U.HandleErr(w, r, err, http.StatusUnauthorized) | ||
return | ||
} | ||
if err := setAuthenticatedCookie(w, creds.Username); err != nil { | ||
U.HandleErr(w, r, err, http.StatusInternalServerError) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
} |
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