oidc.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright 2020 guylewin, guy@lewin.co.il
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package auth
  15. import (
  16. "context"
  17. "fmt"
  18. "github.com/coreos/go-oidc"
  19. "golang.org/x/oauth2/clientcredentials"
  20. "github.com/fatedier/frp/pkg/msg"
  21. )
  22. type OidcClientConfig struct {
  23. // OidcClientID specifies the client ID to use to get a token in OIDC
  24. // authentication if AuthenticationMethod == "oidc". By default, this value
  25. // is "".
  26. OidcClientID string `ini:"oidc_client_id" json:"oidc_client_id"`
  27. // OidcClientSecret specifies the client secret to use to get a token in OIDC
  28. // authentication if AuthenticationMethod == "oidc". By default, this value
  29. // is "".
  30. OidcClientSecret string `ini:"oidc_client_secret" json:"oidc_client_secret"`
  31. // OidcAudience specifies the audience of the token in OIDC authentication
  32. // if AuthenticationMethod == "oidc". By default, this value is "".
  33. OidcAudience string `ini:"oidc_audience" json:"oidc_audience"`
  34. // OidcTokenEndpointURL specifies the URL which implements OIDC Token Endpoint.
  35. // It will be used to get an OIDC token if AuthenticationMethod == "oidc".
  36. // By default, this value is "".
  37. OidcTokenEndpointURL string `ini:"oidc_token_endpoint_url" json:"oidc_token_endpoint_url"`
  38. // OidcAdditionalEndpointParams specifies additional parameters to be sent
  39. // this field will be transfer to map[string][]string in OIDC token generator
  40. // The field will be set by prefix "oidc_additional_"
  41. OidcAdditionalEndpointParams map[string]string `ini:"-" json:"oidc_additional_endpoint_params"`
  42. }
  43. func getDefaultOidcClientConf() OidcClientConfig {
  44. return OidcClientConfig{
  45. OidcClientID: "",
  46. OidcClientSecret: "",
  47. OidcAudience: "",
  48. OidcTokenEndpointURL: "",
  49. OidcAdditionalEndpointParams: make(map[string]string),
  50. }
  51. }
  52. type OidcServerConfig struct {
  53. // OidcIssuer specifies the issuer to verify OIDC tokens with. This issuer
  54. // will be used to load public keys to verify signature and will be compared
  55. // with the issuer claim in the OIDC token. It will be used if
  56. // AuthenticationMethod == "oidc". By default, this value is "".
  57. OidcIssuer string `ini:"oidc_issuer" json:"oidc_issuer"`
  58. // OidcAudience specifies the audience OIDC tokens should contain when validated.
  59. // If this value is empty, audience ("client ID") verification will be skipped.
  60. // It will be used when AuthenticationMethod == "oidc". By default, this
  61. // value is "".
  62. OidcAudience string `ini:"oidc_audience" json:"oidc_audience"`
  63. // OidcSkipExpiryCheck specifies whether to skip checking if the OIDC token is
  64. // expired. It will be used when AuthenticationMethod == "oidc". By default, this
  65. // value is false.
  66. OidcSkipExpiryCheck bool `ini:"oidc_skip_expiry_check" json:"oidc_skip_expiry_check"`
  67. // OidcSkipIssuerCheck specifies whether to skip checking if the OIDC token's
  68. // issuer claim matches the issuer specified in OidcIssuer. It will be used when
  69. // AuthenticationMethod == "oidc". By default, this value is false.
  70. OidcSkipIssuerCheck bool `ini:"oidc_skip_issuer_check" json:"oidc_skip_issuer_check"`
  71. }
  72. func getDefaultOidcServerConf() OidcServerConfig {
  73. return OidcServerConfig{
  74. OidcIssuer: "",
  75. OidcAudience: "",
  76. OidcSkipExpiryCheck: false,
  77. OidcSkipIssuerCheck: false,
  78. }
  79. }
  80. type OidcAuthProvider struct {
  81. BaseConfig
  82. tokenGenerator *clientcredentials.Config
  83. }
  84. func NewOidcAuthSetter(baseCfg BaseConfig, cfg OidcClientConfig) *OidcAuthProvider {
  85. eps := make(map[string][]string)
  86. for k, v := range cfg.OidcAdditionalEndpointParams {
  87. eps[k] = []string{v}
  88. }
  89. tokenGenerator := &clientcredentials.Config{
  90. ClientID: cfg.OidcClientID,
  91. ClientSecret: cfg.OidcClientSecret,
  92. Scopes: []string{cfg.OidcAudience},
  93. TokenURL: cfg.OidcTokenEndpointURL,
  94. EndpointParams: eps,
  95. }
  96. return &OidcAuthProvider{
  97. BaseConfig: baseCfg,
  98. tokenGenerator: tokenGenerator,
  99. }
  100. }
  101. func (auth *OidcAuthProvider) generateAccessToken() (accessToken string, err error) {
  102. tokenObj, err := auth.tokenGenerator.Token(context.Background())
  103. if err != nil {
  104. return "", fmt.Errorf("couldn't generate OIDC token for login: %v", err)
  105. }
  106. return tokenObj.AccessToken, nil
  107. }
  108. func (auth *OidcAuthProvider) SetLogin(loginMsg *msg.Login) (err error) {
  109. loginMsg.PrivilegeKey, err = auth.generateAccessToken()
  110. return err
  111. }
  112. func (auth *OidcAuthProvider) SetPing(pingMsg *msg.Ping) (err error) {
  113. if !auth.AuthenticateHeartBeats {
  114. return nil
  115. }
  116. pingMsg.PrivilegeKey, err = auth.generateAccessToken()
  117. return err
  118. }
  119. func (auth *OidcAuthProvider) SetNewWorkConn(newWorkConnMsg *msg.NewWorkConn) (err error) {
  120. if !auth.AuthenticateNewWorkConns {
  121. return nil
  122. }
  123. newWorkConnMsg.PrivilegeKey, err = auth.generateAccessToken()
  124. return err
  125. }
  126. type OidcAuthConsumer struct {
  127. BaseConfig
  128. verifier *oidc.IDTokenVerifier
  129. subjectFromLogin string
  130. }
  131. func NewOidcAuthVerifier(baseCfg BaseConfig, cfg OidcServerConfig) *OidcAuthConsumer {
  132. provider, err := oidc.NewProvider(context.Background(), cfg.OidcIssuer)
  133. if err != nil {
  134. panic(err)
  135. }
  136. verifierConf := oidc.Config{
  137. ClientID: cfg.OidcAudience,
  138. SkipClientIDCheck: cfg.OidcAudience == "",
  139. SkipExpiryCheck: cfg.OidcSkipExpiryCheck,
  140. SkipIssuerCheck: cfg.OidcSkipIssuerCheck,
  141. }
  142. return &OidcAuthConsumer{
  143. BaseConfig: baseCfg,
  144. verifier: provider.Verifier(&verifierConf),
  145. }
  146. }
  147. func (auth *OidcAuthConsumer) VerifyLogin(loginMsg *msg.Login) (err error) {
  148. token, err := auth.verifier.Verify(context.Background(), loginMsg.PrivilegeKey)
  149. if err != nil {
  150. return fmt.Errorf("invalid OIDC token in login: %v", err)
  151. }
  152. auth.subjectFromLogin = token.Subject
  153. return nil
  154. }
  155. func (auth *OidcAuthConsumer) verifyPostLoginToken(privilegeKey string) (err error) {
  156. token, err := auth.verifier.Verify(context.Background(), privilegeKey)
  157. if err != nil {
  158. return fmt.Errorf("invalid OIDC token in ping: %v", err)
  159. }
  160. if token.Subject != auth.subjectFromLogin {
  161. return fmt.Errorf("received different OIDC subject in login and ping. "+
  162. "original subject: %s, "+
  163. "new subject: %s",
  164. auth.subjectFromLogin, token.Subject)
  165. }
  166. return nil
  167. }
  168. func (auth *OidcAuthConsumer) VerifyPing(pingMsg *msg.Ping) (err error) {
  169. if !auth.AuthenticateHeartBeats {
  170. return nil
  171. }
  172. return auth.verifyPostLoginToken(pingMsg.PrivilegeKey)
  173. }
  174. func (auth *OidcAuthConsumer) VerifyNewWorkConn(newWorkConnMsg *msg.NewWorkConn) (err error) {
  175. if !auth.AuthenticateNewWorkConns {
  176. return nil
  177. }
  178. return auth.verifyPostLoginToken(newWorkConnMsg.PrivilegeKey)
  179. }