This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
forked from vmware-archive/skymarshal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskymarshal.go
131 lines (117 loc) · 3.13 KB
/
skymarshal.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
package skymarshal
import (
"crypto/rsa"
"net/http"
"time"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc/db"
"github.com/concourse/skymarshal/auth"
"github.com/concourse/skymarshal/authserver"
)
type Config struct {
BaseUrl string
BaseAuthUrl string
SigningKey *rsa.PrivateKey
Expiration time.Duration
CookieSecure bool
IsTLSEnabled bool
TeamFactory db.TeamFactory
Logger lager.Logger
}
type DetailedConfig struct {
BaseUrl string
BaseAuthUrl string
Expiration time.Duration
CookieSecure bool
IsTLSEnabled bool
TeamFactory db.TeamFactory
Logger lager.Logger
OAuthFactory auth.ProviderFactory
OAuthFactoryV1 auth.ProviderFactory
TokenReader auth.TokenReader
TokenValidator auth.TokenValidator
BasicAuthValidator auth.TokenValidator
CSRFTokenGenerator auth.CSRFTokenGenerator
AuthTokenGenerator auth.AuthTokenGenerator
}
func NewHandler(config *Config) (http.Handler, error) {
config.Logger = config.Logger.Session("skymarshal")
oauthFactory := auth.NewOAuthFactory(
config.Logger.Session("oauth-provider-factory"),
config.BaseAuthUrl,
auth.Routes,
auth.OAuthCallback,
)
oauthFactoryV1 := auth.NewOAuthFactory(
config.Logger.Session("oauth-v1-provider-factory"),
config.BaseAuthUrl,
auth.V1Routes,
auth.OAuthV1Callback,
)
return NewHandlerWithOptions(&DetailedConfig{
config.BaseUrl,
config.BaseAuthUrl,
config.Expiration,
config.CookieSecure,
config.IsTLSEnabled,
config.TeamFactory,
config.Logger,
oauthFactory,
oauthFactoryV1,
auth.JWTReader{&config.SigningKey.PublicKey},
auth.JWTValidator{&config.SigningKey.PublicKey},
auth.NewBasicAuthValidator(config.Logger, config.TeamFactory),
auth.NewCSRFTokenGenerator(),
auth.NewAuthTokenGenerator(config.SigningKey),
})
}
func NewHandlerWithOptions(config *DetailedConfig) (http.Handler, error) {
oauthHandler, err := auth.NewOAuthHandler(
config.Logger,
config.OAuthFactory,
config.TeamFactory,
config.CSRFTokenGenerator,
config.AuthTokenGenerator,
config.Expiration,
config.CookieSecure,
config.IsTLSEnabled,
)
if err != nil {
return nil, err
}
oauthV1Handler, err := auth.NewOAuthV1Handler(
config.Logger,
config.OAuthFactoryV1,
config.TeamFactory,
config.CSRFTokenGenerator,
config.AuthTokenGenerator,
config.Expiration,
config.CookieSecure,
config.IsTLSEnabled,
)
if err != nil {
return nil, err
}
authServer := authserver.NewServer(
config.Logger,
config.BaseUrl,
config.BaseAuthUrl,
config.Expiration,
config.CookieSecure,
config.IsTLSEnabled,
config.TeamFactory,
config.OAuthFactory,
config.CSRFTokenGenerator,
config.AuthTokenGenerator,
config.TokenReader,
config.TokenValidator,
config.BasicAuthValidator,
)
webMux := http.NewServeMux()
webMux.Handle("/auth/list_methods", http.HandlerFunc(authServer.ListAuthMethods))
webMux.Handle("/auth/basic/token", http.HandlerFunc(authServer.GetAuthToken))
webMux.Handle("/auth/userinfo", http.HandlerFunc(authServer.GetUser))
webMux.Handle("/auth/", oauthHandler)
webMux.Handle("/oauth/v1/", oauthV1Handler)
return webMux, nil
}