This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
oidc-api.go
165 lines (131 loc) · 4.37 KB
/
oidc-api.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package server
import (
"context"
"fmt"
"github.com/openziti/edge/controller"
"github.com/openziti/edge/controller/env"
"github.com/openziti/edge/controller/oidc_auth"
"github.com/openziti/fabric/controller/api"
"github.com/openziti/foundation/v2/stringz"
"github.com/openziti/xweb/v2"
"net/http"
"strings"
)
var _ xweb.ApiHandlerFactory = &OidcApiFactory{}
type OidcApiFactory struct {
InitFunc func(*OidcApiHandler) error
appEnv *env.AppEnv
}
func (factory OidcApiFactory) Validate(config *xweb.InstanceConfig) error {
return nil
}
func NewOidcApiFactory(appEnv *env.AppEnv) *OidcApiFactory {
return &OidcApiFactory{
appEnv: appEnv,
}
}
func (factory OidcApiFactory) Binding() string {
return controller.OidcApiBinding
}
func (factory OidcApiFactory) New(serverConfig *xweb.ServerConfig, options map[interface{}]interface{}) (xweb.ApiHandler, error) {
oidcApi, err := NewOidcApiHandler(serverConfig, factory.appEnv, options)
if err != nil {
return nil, err
}
if factory.InitFunc != nil {
if err := factory.InitFunc(oidcApi); err != nil {
return nil, fmt.Errorf("error running on init func: %v", err)
}
}
return oidcApi, nil
}
type OidcApiHandler struct {
handler http.Handler
appEnv *env.AppEnv
options map[interface{}]interface{}
}
func (h OidcApiHandler) Binding() string {
return controller.OidcApiBinding
}
func (h OidcApiHandler) Options() map[interface{}]interface{} {
return h.options
}
func (h OidcApiHandler) RootPath() string {
return "/oidc"
}
func (h OidcApiHandler) IsHandler(r *http.Request) bool {
return strings.HasPrefix(r.URL.Path, h.RootPath()) || strings.HasPrefix(r.URL.Path, oidc_auth.WellKnownOidcConfiguration)
}
func (h OidcApiHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
h.handler.ServeHTTP(writer, request)
}
func (h OidcApiHandler) IsDefault() bool {
return true
}
func NewOidcApiHandler(serverConfig *xweb.ServerConfig, ae *env.AppEnv, options map[interface{}]interface{}) (*OidcApiHandler, error) {
oidcApi := &OidcApiHandler{
options: options,
appEnv: ae,
}
serverCert := serverConfig.Identity.ServerCert()
cert := serverCert[0].Leaf
key := serverCert[0].PrivateKey
issuer := "https://" + ae.Config.Api.Address + "/oidc"
oidcConfig := oidc_auth.NewConfig(issuer, cert, key)
if secretVal, ok := options["secret"]; ok {
if secret, ok := secretVal.(string); ok {
secret = strings.TrimSpace(secret)
if secret == "" {
return nil, fmt.Errorf("[edge-oidc.options.secret] must not be empty")
}
oidcConfig.TokenSecret = secret
} else {
return nil, fmt.Errorf("[edge-oidc.options.secret] must be a string")
}
} else {
return nil, fmt.Errorf("[edge-oidc.options.secret] must be definded")
}
if redirectVal, ok := options["redirectURIs"]; ok {
if redirects, ok := redirectVal.([]interface{}); ok {
for _, redirectVal := range redirects {
if redirect, ok := redirectVal.(string); ok {
oidcConfig.RedirectURIs = append(oidcConfig.RedirectURIs, redirect)
}
}
}
}
if postLogoutVal, ok := options["postLogoutURIs"]; ok {
if postLogs, ok := postLogoutVal.([]interface{}); ok {
for _, postLogVal := range postLogs {
if postLog, ok := postLogVal.(string); ok {
oidcConfig.PostLogoutURIs = append(oidcConfig.PostLogoutURIs, postLog)
}
}
}
}
if !stringz.Contains(oidcConfig.RedirectURIs, "openziti://auth/callback") {
oidcConfig.RedirectURIs = append(oidcConfig.RedirectURIs, "openziti://auth/callback")
}
if !stringz.Contains(oidcConfig.PostLogoutURIs, "openziti://auth/logout") {
oidcConfig.PostLogoutURIs = append(oidcConfig.PostLogoutURIs, "openziti://auth/logout")
}
var err error
oidcApi.handler, err = oidc_auth.NewNativeOnlyOP(context.Background(), ae, oidcConfig)
if err != nil {
return nil, err
}
oidcApi.handler = api.WrapCorsHandler(oidcApi.handler)
return oidcApi, nil
}