-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
111 lines (84 loc) · 2.96 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/quakenroll/doxen/graph"
doxen "github.com/quakenroll/doxen/graph/generated"
"github.com/quakenroll/doxen/internal/firebase/authdoxen"
database "github.com/quakenroll/doxen/internal/pkg/db/mysql"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/go-chi/chi"
)
const defaultPort = "8081"
var googleOauthConfig = oauth2.Config{
RedirectURL: "https://localhost:8081/auth/google/callback",
ClientID: "1017766501798-6cq9t9iirccri93udjffg9s8a8esrn28.apps.googleusercontent.com",
ClientSecret: "ayoClDJ9bgDFNbjKPq8TzOO9",
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email"},
Endpoint: google.Endpoint,
}
type googleLoginHandler struct {
}
func (d googleLoginHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
url := googleOauthConfig.AuthCodeURL("MyAuthVerificationString")
fmt.Fprint(w, string(url))
//log.Println(url)
//http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
type googleAuthCallbackHandler struct {
}
func (d googleAuthCallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
value := r.FormValue("state")
if value != "MyAuthVerificationString" {
log.Println("invalid google oauth state", value)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
data, err := getGoogleUserInfo(r.FormValue("code"))
if err != nil {
log.Println(err.Error())
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
fmt.Fprint(w, string(data))
}
const oauthGoogleUrlAPI = "https://www.googleapis.com/oauth2/v2/userinfo?access_token="
func getGoogleUserInfo(code string) ([]byte, error) {
token, err := googleOauthConfig.Exchange(context.Background(), code)
if err != nil {
return nil, fmt.Errorf("Failed to Exchange %s\n", err.Error())
}
resp, err := http.Get(oauthGoogleUrlAPI + token.AccessToken)
if err != nil {
return nil, fmt.Errorf("Failed to Get UserInfo from Google %s\n", err.Error())
}
return ioutil.ReadAll(resp.Body)
}
func main() {
//generatepem.GenFile()
authdoxen.VerifyIDToken()
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
router := chi.NewRouter()
//router.Use(auth.Middleware()) // this sets the handler that should be processed before any normal handler's routines
database.InitDB()
database.Migrate()
server := handler.NewDefaultServer(doxen.NewExecutableSchema(doxen.Config{Resolvers: &graph.Resolver{}}))
router.Handle("/auth/google/login", googleLoginHandler{})
router.Handle("/auth/google/callback", googleAuthCallbackHandler{})
router.Handle("/", playground.Handler("GraphQL playground", "/query"))
router.Handle("/query", server)
log.Printf("connect to https://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServeTLS(":"+port,
"../certification/localhost.crt",
"../certification/localhost.key", router))
}