-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (71 loc) · 2.08 KB
/
main.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
package main
import (
"context"
"errors"
"log"
"net/http"
"os"
"buf.build/gen/go/pocketsign/apis/connectrpc/go/pocketsign/link/v1/linkv1connect"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/gorilla/sessions"
"github.com/joho/godotenv"
"golang.org/x/oauth2"
)
// 取得してきたAccessTokenを保存する
var tokenSourceStore = map[string]oauth2.TokenSource{}
var refreshTokenStore = map[string]string{}
var errNotAuthenticated = errors.New("not authenticated")
// カスタムリソース
var customResourceID string
var subscriberAPIConnectClient linkv1connect.SubscriberServiceClient
type handler struct {
conf *oauth2.Config
provider *oidc.Provider
store sessions.Store
}
type UserResource struct {
Value string `json:"value"`
}
func main() {
ctx := context.Background()
// 環境変数の読み込み
err := godotenv.Load()
if err != nil {
log.Fatal(err)
}
host := os.Getenv("OIDC_DEMO_HOST")
// Subscriber APIのConnect Clientを初期化
subscriberAPIConnectClient = linkv1connect.NewSubscriberServiceClient(
http.DefaultClient,
"https://api."+host,
)
provider, err := oidc.NewProvider(ctx, "https://oidc."+host)
if err != nil {
log.Fatal(err)
}
// OAuth2 Clientの設定
oauth2Config := oauth2.Config{
ClientID: os.Getenv("OIDC_CLIENT_ID"),
ClientSecret: os.Getenv("OIDC_CLIENT_SECRET"),
RedirectURL: os.Getenv("OIDC_REDIRECT_URL"),
Endpoint: provider.Endpoint(),
Scopes: []string{oidc.ScopeOpenID, oidc.ScopeOfflineAccess},
}
// Sessionの設定(Cookieに保存)
store := sessions.NewCookieStore([]byte(os.Getenv("SESSION_SECRET")))
// 自分で定義したリソースIDの取得
customResourceID = os.Getenv("CUSTOM_RESOURCE_ID")
h := &handler{
conf: &oauth2Config,
provider: provider,
store: store,
}
// ハンドラの設定
http.HandleFunc("/", h.handleIndex)
http.HandleFunc("/update", h.handlePut)
http.HandleFunc("/login", h.handleRedirect)
http.HandleFunc("/callback", h.handleCallback)
http.HandleFunc("/refresh", h.handleRefresh)
// サーバーの起動
log.Fatal(http.ListenAndServe(":8080", nil))
}