Skip to content

Commit f42ccb7

Browse files
committed
feat: refactor Linuxdo authentication to use config values for client ID, secret, and redirect URI
1 parent a017168 commit f42ccb7

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

linuxdo.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@ const (
1717
UserEndpoint = "https://connect.linux.do/api/user"
1818
)
1919

20-
var (
21-
ClientID = config.LinuxdoClientId
22-
ClientSecret = config.LinuxdoClientSecret
23-
RedirectURI = config.Adderss + "/oauth2/callback"
24-
store = sessions.NewCookieStore([]byte("secret-key"))
25-
)
20+
var store = sessions.NewCookieStore([]byte("secret-key"))
2621

2722
type User struct {
2823
ID int `json:"id"`
@@ -53,12 +48,12 @@ func initiateAuthHandler(w http.ResponseWriter, r *http.Request) {
5348
// 构造授权 URL
5449
authURL := fmt.Sprintf("%s?client_id=%s&response_type=code&redirect_uri=%s&state=%s",
5550
AuthorizationEndpoint,
56-
ClientID,
57-
RedirectURI,
51+
config.ClientId,
52+
config.Adderss+"/oauth2/callback",
5853
state,
5954
)
60-
61-
http.Redirect(w, r, authURL, http.StatusFound)
55+
fmt.Printf("authURL: %v\n", authURL)
56+
// http.Redirect(w, r, authURL, http.StatusFound)
6257
}
6358

6459
// 处理回调
@@ -81,12 +76,12 @@ func callbackHandler(w http.ResponseWriter, r *http.Request) {
8176

8277
// 请求 access token
8378
resp, err := client.R().
84-
SetBasicAuth(ClientID, ClientSecret).
79+
SetBasicAuth(config.ClientId, config.ClientSecret).
8580
SetHeader("Accept", "application/json").
8681
SetFormData(map[string]string{
8782
"grant_type": "authorization_code",
8883
"code": code,
89-
"redirect_uri": RedirectURI,
84+
"redirect_uri": config.Adderss + "/oauth2/callback",
9085
}).
9186
Post(TokenEndpoint)
9287

0 commit comments

Comments
 (0)