update electron android adb over browser

This commit is contained in:
Kenta420 2024-01-19 17:53:48 +07:00
parent 21109e4bf9
commit f6295a9c2f
26 changed files with 1551 additions and 172 deletions

View file

@ -1,10 +1,11 @@
package config
type ServerConfig struct {
ServerPort uint `mapstructure:"SERVER_PORT"`
AllowedOrigins string `mapstructure:"ALLOWED_ORIGINS"`
ClientRedirectURL string `mapstructure:"CLIENT_REDIRECT_URL"`
ServerDomain string `mapstructure:"SERVER_DOMAIN"`
APIKey string `mapstructure:"API_KEY"`
Debug bool `mapstructure:"DEBUG_MODE"`
ServerPort uint `mapstructure:"SERVER_PORT"`
AllowedOrigins string `mapstructure:"ALLOWED_ORIGINS"`
ClientRedirectURL string `mapstructure:"CLIENT_REDIRECT_URL"`
ClientElectronRedirectURL string `mapstructure:"CLIENT_ELECTRON_REDIRECT_URL"`
ServerDomain string `mapstructure:"SERVER_DOMAIN"`
APIKey string `mapstructure:"API_KEY"`
Debug bool `mapstructure:"DEBUG_MODE"`
}

View file

@ -4,9 +4,7 @@ import (
"context"
"crypto/rand"
"encoding/base64"
"github.com/go-chi/chi/v5"
"go.uber.org/zap"
"golang.org/x/oauth2"
"encoding/json"
"net/http"
"net/url"
"recipe-manager/config"
@ -15,6 +13,10 @@ import (
"recipe-manager/services/user"
"strconv"
"time"
"github.com/go-chi/chi/v5"
"go.uber.org/zap"
"golang.org/x/oauth2"
)
type AuthRouter struct {
@ -42,6 +44,10 @@ func (ar *AuthRouter) Route(r chi.Router) {
stateMap["redirect_to"] = r.URL.Query().Get("redirect_to")
}
if r.URL.Query().Get("kind") != "" {
stateMap["kind"] = r.URL.Query().Get("kind")
}
authURL := ar.oauth.AuthURL(state, stateMap)
http.Redirect(w, r, authURL, http.StatusTemporaryRedirect)
})
@ -52,6 +58,7 @@ func (ar *AuthRouter) Route(r chi.Router) {
defer cancel()
var redirectTo string
var kind string
state := r.URL.Query().Get("state")
if state == "" {
http.Error(w, "State not found", http.StatusBadRequest)
@ -65,6 +72,7 @@ func (ar *AuthRouter) Route(r chi.Router) {
}
redirectTo = val["redirect_to"]
kind = val["kind"]
ar.oauth.RemoveState(state)
}
@ -115,12 +123,62 @@ func (ar *AuthRouter) Route(r chi.Router) {
ar.taoLogger.Log.Info("User Log-In Success", zap.String("userInfo", userInfo.Name), zap.String("email", userInfo.Email))
if kind == "electron" {
value.Add("access_token", token.AccessToken)
value.Add("max_age", "3600")
value.Add("refresh_token", token.RefreshToken)
http.Redirect(w, r, ar.cfg.ClientElectronRedirectURL+"login?"+value.Encode(), http.StatusTemporaryRedirect)
return
}
// redirect to frontend with token and refresh token
w.Header().Add("set-cookie", "access_token="+token.AccessToken+"; Path=/; HttpOnly; SameSite=None; Secure; Max-Age=3600")
w.Header().Add("set-cookie", "refresh_token="+token.RefreshToken+"; Path=/; HttpOnly; SameSite=None; Secure")
http.Redirect(w, r, ar.cfg.ClientRedirectURL+"/?"+value.Encode(), http.StatusTemporaryRedirect)
})
r.Get("/me", func(w http.ResponseWriter, r *http.Request) {
// get access token from cookie
cookie, err := r.Cookie("access_token")
if err != nil {
http.Error(w, "Access token not found", http.StatusBadRequest)
return
}
// get userInfo info
userInfo, err := ar.oauth.GetUserInfo(r.Context(), &oauth2.Token{AccessToken: cookie.Value})
if err != nil {
http.Error(w, "Error getting userInfo info", http.StatusBadRequest)
return
}
// map with database
userFromDb, err := ar.userService.GetUserByEmail(r.Context(), userInfo.Email)
if err != nil {
http.Error(w, "Error while getting user data from database.", http.StatusInternalServerError)
return
}
if userFromDb == nil {
http.Error(w, "Unauthorized, We not found your email, Please contact admin.", http.StatusUnauthorized)
return
}
picture := userInfo.Picture
if userFromDb.Picture != "" {
picture = userFromDb.Picture
}
value := url.Values{
"id": {userFromDb.ID},
"name": {userFromDb.Name},
"email": {userInfo.Email},
"picture": {picture},
"permissions": {strconv.Itoa(int(userFromDb.Permissions))},
}
json.NewEncoder(w).Encode(value)
})
r.Get("/refresh", func(w http.ResponseWriter, r *http.Request) {
// get refresh token from query string
refreshToken := r.URL.Query().Get("refresh_token")