mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
update github oauth callback api
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@ -13,6 +13,9 @@
|
|||||||
*.log
|
*.log
|
||||||
tmp
|
tmp
|
||||||
|
|
||||||
|
# Config
|
||||||
|
config
|
||||||
|
|
||||||
# Air (hot reload) generated
|
# Air (hot reload) generated
|
||||||
.air
|
.air
|
||||||
|
|
||||||
|
127
api/auth.go
127
api/auth.go
@ -1,8 +1,14 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"memos/api/e"
|
"memos/api/e"
|
||||||
|
"memos/common"
|
||||||
|
"memos/config"
|
||||||
"memos/store"
|
"memos/store"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@ -88,10 +94,131 @@ func handleUserSignOut(w http.ResponseWriter, r *http.Request) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleGithubAuthCallback(w http.ResponseWriter, r *http.Request) {
|
||||||
|
code := r.URL.Query().Get("code")
|
||||||
|
|
||||||
|
requestBody := map[string]string{
|
||||||
|
"client_id": config.GITHUB_CLIENTID,
|
||||||
|
"client_secret": config.GITHUB_SECRET,
|
||||||
|
"code": code,
|
||||||
|
}
|
||||||
|
|
||||||
|
requestJSON, _ := json.Marshal(requestBody)
|
||||||
|
|
||||||
|
// POST request to get access_token
|
||||||
|
req, err := http.NewRequest(
|
||||||
|
"POST",
|
||||||
|
"https://github.com/login/oauth/access_token",
|
||||||
|
bytes.NewBuffer(requestJSON),
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
e.ErrorHandler(w, "REQUEST_BODY_ERROR", "Error in request github api")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("Accept", "application/json")
|
||||||
|
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
e.ErrorHandler(w, "REQUEST_BODY_ERROR", "Error in request github api")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Response body converted to stringified JSON
|
||||||
|
respBody, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
|
||||||
|
// Represents the response received from Github
|
||||||
|
type GithubAccessTokenResponse struct {
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
TokenType string `json:"token_type"`
|
||||||
|
Scope string `json:"scope"`
|
||||||
|
}
|
||||||
|
|
||||||
|
ghResp := GithubAccessTokenResponse{}
|
||||||
|
json.Unmarshal(respBody, &ghResp)
|
||||||
|
|
||||||
|
githubAccessToken := ghResp.AccessToken
|
||||||
|
|
||||||
|
// Get request to a set URL
|
||||||
|
req, err = http.NewRequest(
|
||||||
|
"GET",
|
||||||
|
"https://api.github.com/user",
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
e.ErrorHandler(w, "REQUEST_BODY_ERROR", "Error in request github api")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
authorizationHeaderValue := fmt.Sprintf("token %s", githubAccessToken)
|
||||||
|
req.Header.Set("Authorization", authorizationHeaderValue)
|
||||||
|
|
||||||
|
resp, err = http.DefaultClient.Do(req)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
e.ErrorHandler(w, "REQUEST_BODY_ERROR", "Error in request github api")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
respBody, _ = ioutil.ReadAll(resp.Body)
|
||||||
|
|
||||||
|
githubData := string(respBody)
|
||||||
|
|
||||||
|
type GithubUser struct {
|
||||||
|
Login string `json:"login"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
githubUser := GithubUser{}
|
||||||
|
json.Unmarshal([]byte(githubData), &githubUser)
|
||||||
|
|
||||||
|
session, _ := SessionStore.Get(r, "session")
|
||||||
|
userId := fmt.Sprintf("%v", session.Values["user_id"])
|
||||||
|
|
||||||
|
if userId != "" {
|
||||||
|
githubNameUsable, err := store.CheckGithubNameUsable(githubUser.Login)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
e.ErrorHandler(w, "DATABASE_ERROR", "Error in CheckGithubNameUsable")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !githubNameUsable {
|
||||||
|
e.ErrorHandler(w, "DATABASE_ERROR", "Error in CheckGithubNameUsable")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
userPatch := store.UserPatch{
|
||||||
|
GithubName: &githubUser.Login,
|
||||||
|
}
|
||||||
|
|
||||||
|
store.UpdateUser(userId, &userPatch)
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := store.GetUserByGithubName(githubUser.Login)
|
||||||
|
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
username := githubUser.Name
|
||||||
|
usernameUsable, _ := store.CheckUsernameUsable(username)
|
||||||
|
if !usernameUsable {
|
||||||
|
username = username + common.GenUUID()
|
||||||
|
}
|
||||||
|
user, _ = store.CreateNewUser(username, username, githubUser.Login, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
session.Values["user_id"] = user.Id
|
||||||
|
session.Save(r, w)
|
||||||
|
|
||||||
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
||||||
|
}
|
||||||
|
|
||||||
func RegisterAuthRoutes(r *mux.Router) {
|
func RegisterAuthRoutes(r *mux.Router) {
|
||||||
authRouter := r.PathPrefix("/api/auth").Subrouter()
|
authRouter := r.PathPrefix("/api/auth").Subrouter()
|
||||||
|
|
||||||
authRouter.HandleFunc("/signup", handleUserSignUp).Methods("POST")
|
authRouter.HandleFunc("/signup", handleUserSignUp).Methods("POST")
|
||||||
authRouter.HandleFunc("/signin", handleUserSignIn).Methods("POST")
|
authRouter.HandleFunc("/signin", handleUserSignIn).Methods("POST")
|
||||||
authRouter.HandleFunc("/signout", handleUserSignOut).Methods("POST")
|
authRouter.HandleFunc("/signout", handleUserSignOut).Methods("POST")
|
||||||
|
authRouter.HandleFunc("/github", handleGithubAuthCallback).Methods("GET")
|
||||||
}
|
}
|
||||||
|
2
main.go
2
main.go
@ -13,8 +13,8 @@ func main() {
|
|||||||
|
|
||||||
r := mux.NewRouter().StrictSlash(true)
|
r := mux.NewRouter().StrictSlash(true)
|
||||||
|
|
||||||
api.RegisterUserRoutes(r)
|
|
||||||
api.RegisterAuthRoutes(r)
|
api.RegisterAuthRoutes(r)
|
||||||
|
api.RegisterUserRoutes(r)
|
||||||
api.RegisterMemoRoutes(r)
|
api.RegisterMemoRoutes(r)
|
||||||
api.RegisterQueryRoutes(r)
|
api.RegisterQueryRoutes(r)
|
||||||
|
|
||||||
|
Binary file not shown.
@ -3,7 +3,6 @@ import appContext from "./stores/appContext";
|
|||||||
import { appRouterSwitch } from "./routers";
|
import { appRouterSwitch } from "./routers";
|
||||||
import { globalStateService } from "./services";
|
import { globalStateService } from "./services";
|
||||||
import "./less/app.less";
|
import "./less/app.less";
|
||||||
import 'prismjs/themes/prism.css';
|
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const {
|
const {
|
||||||
|
@ -155,7 +155,7 @@ const MyAccountSection: React.FC<Props> = () => {
|
|||||||
<Only when={window.location.origin.includes("justsven.top")}>
|
<Only when={window.location.origin.includes("justsven.top")}>
|
||||||
<div className="section-container connect-section-container">
|
<div className="section-container connect-section-container">
|
||||||
<p className="title-text">关联账号</p>
|
<p className="title-text">关联账号</p>
|
||||||
<label className="form-label input-form-label">
|
<label className="form-label input-form-label hidden">
|
||||||
<span className="normal-text">微信 OpenID:</span>
|
<span className="normal-text">微信 OpenID:</span>
|
||||||
{user.wxOpenId ? (
|
{user.wxOpenId ? (
|
||||||
<>
|
<>
|
||||||
|
Reference in New Issue
Block a user