oidc.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/fatedier/frp/models/msg"
  19. "github.com/coreos/go-oidc"
  20. "github.com/vaughan0/go-ini"
  21. "golang.org/x/oauth2/clientcredentials"
  22. )
  23. type oidcClientConfig struct {
  24. // OidcClientID specifies the client ID to use to get a token in OIDC
  25. // authentication if AuthenticationMethod == "oidc". By default, this value
  26. // is "".
  27. OidcClientID string `json:"oidc_client_id"`
  28. // OidcClientSecret specifies the client secret to use to get a token in OIDC
  29. // authentication if AuthenticationMethod == "oidc". By default, this value
  30. // is "".
  31. OidcClientSecret string `json:"oidc_client_secret"`
  32. // OidcAudience specifies the audience of the token in OIDC authentication
  33. //if AuthenticationMethod == "oidc". By default, this value is "".
  34. OidcAudience string `json:"oidc_audience"`
  35. // OidcTokenEndpointURL specifies the URL which implements OIDC Token Endpoint.
  36. // It will be used to get an OIDC token if AuthenticationMethod == "oidc".
  37. // By default, this value is "".
  38. OidcTokenEndpointURL string `json:"oidc_token_endpoint_url"`
  39. }
  40. func getDefaultOidcClientConf() oidcClientConfig {
  41. return oidcClientConfig{
  42. OidcClientID: "",
  43. OidcClientSecret: "",
  44. OidcAudience: "",
  45. OidcTokenEndpointURL: "",
  46. }
  47. }
  48. func unmarshalOidcClientConfFromIni(conf ini.File) oidcClientConfig {
  49. var (
  50. tmpStr string
  51. ok bool
  52. )
  53. cfg := getDefaultOidcClientConf()
  54. if tmpStr, ok = conf.Get("common", "oidc_client_id"); ok {
  55. cfg.OidcClientID = tmpStr
  56. }
  57. if tmpStr, ok = conf.Get("common", "oidc_client_secret"); ok {
  58. cfg.OidcClientSecret = tmpStr
  59. }
  60. if tmpStr, ok = conf.Get("common", "oidc_audience"); ok {
  61. cfg.OidcAudience = tmpStr
  62. }
  63. if tmpStr, ok = conf.Get("common", "oidc_token_endpoint_url"); ok {
  64. cfg.OidcTokenEndpointURL = tmpStr
  65. }
  66. return cfg
  67. }
  68. type oidcServerConfig struct {
  69. // OidcIssuer specifies the issuer to verify OIDC tokens with. This issuer
  70. // will be used to load public keys to verify signature and will be compared
  71. // with the issuer claim in the OIDC token. It will be used if
  72. // AuthenticationMethod == "oidc". By default, this value is "".
  73. OidcIssuer string `json:"oidc_issuer"`
  74. // OidcAudience specifies the audience OIDC tokens should contain when validated.
  75. // If this value is empty, audience ("client ID") verification will be skipped.
  76. // It will be used when AuthenticationMethod == "oidc". By default, this
  77. // value is "".
  78. OidcAudience string `json:"oidc_audience"`
  79. // OidcSkipExpiryCheck specifies whether to skip checking if the OIDC token is
  80. // expired. It will be used when AuthenticationMethod == "oidc". By default, this
  81. // value is false.
  82. OidcSkipExpiryCheck bool `json:"oidc_skip_expiry_check"`
  83. // OidcSkipIssuerCheck specifies whether to skip checking if the OIDC token's
  84. // issuer claim matches the issuer specified in OidcIssuer. It will be used when
  85. // AuthenticationMethod == "oidc". By default, this value is false.
  86. OidcSkipIssuerCheck bool `json:"oidc_skip_issuer_check"`
  87. }
  88. func getDefaultOidcServerConf() oidcServerConfig {
  89. return oidcServerConfig{
  90. OidcIssuer: "",
  91. OidcAudience: "",
  92. OidcSkipExpiryCheck: false,
  93. OidcSkipIssuerCheck: false,
  94. }
  95. }
  96. func unmarshalOidcServerConfFromIni(conf ini.File) oidcServerConfig {
  97. var (
  98. tmpStr string
  99. ok bool
  100. )
  101. cfg := getDefaultOidcServerConf()
  102. if tmpStr, ok = conf.Get("common", "oidc_issuer"); ok {
  103. cfg.OidcIssuer = tmpStr
  104. }
  105. if tmpStr, ok = conf.Get("common", "oidc_audience"); ok {
  106. cfg.OidcAudience = tmpStr
  107. }
  108. if tmpStr, ok = conf.Get("common", "oidc_skip_expiry_check"); ok && tmpStr == "true" {
  109. cfg.OidcSkipExpiryCheck = true
  110. } else {
  111. cfg.OidcSkipExpiryCheck = false
  112. }
  113. if tmpStr, ok = conf.Get("common", "oidc_skip_issuer_check"); ok && tmpStr == "true" {
  114. cfg.OidcSkipIssuerCheck = true
  115. } else {
  116. cfg.OidcSkipIssuerCheck = false
  117. }
  118. return cfg
  119. }
  120. type OidcAuthProvider struct {
  121. baseConfig
  122. tokenGenerator *clientcredentials.Config
  123. }
  124. func NewOidcAuthSetter(baseCfg baseConfig, cfg oidcClientConfig) *OidcAuthProvider {
  125. tokenGenerator := &clientcredentials.Config{
  126. ClientID: cfg.OidcClientID,
  127. ClientSecret: cfg.OidcClientSecret,
  128. Scopes: []string{cfg.OidcAudience},
  129. TokenURL: cfg.OidcTokenEndpointURL,
  130. }
  131. return &OidcAuthProvider{
  132. baseConfig: baseCfg,
  133. tokenGenerator: tokenGenerator,
  134. }
  135. }
  136. func (auth *OidcAuthProvider) generateAccessToken() (accessToken string, err error) {
  137. tokenObj, err := auth.tokenGenerator.Token(context.Background())
  138. if err != nil {
  139. return "", fmt.Errorf("couldn't generate OIDC token for login: %v", err)
  140. }
  141. return tokenObj.AccessToken, nil
  142. }
  143. func (auth *OidcAuthProvider) SetLogin(loginMsg *msg.Login) (err error) {
  144. loginMsg.PrivilegeKey, err = auth.generateAccessToken()
  145. return err
  146. }
  147. func (auth *OidcAuthProvider) SetPing(pingMsg *msg.Ping) (err error) {
  148. if !auth.AuthenticateHeartBeats {
  149. return nil
  150. }
  151. pingMsg.PrivilegeKey, err = auth.generateAccessToken()
  152. return err
  153. }
  154. func (auth *OidcAuthProvider) SetNewWorkConn(newWorkConnMsg *msg.NewWorkConn) (err error) {
  155. if !auth.AuthenticateNewWorkConns {
  156. return nil
  157. }
  158. newWorkConnMsg.PrivilegeKey, err = auth.generateAccessToken()
  159. return err
  160. }
  161. type OidcAuthConsumer struct {
  162. baseConfig
  163. verifier *oidc.IDTokenVerifier
  164. subjectFromLogin string
  165. }
  166. func NewOidcAuthVerifier(baseCfg baseConfig, cfg oidcServerConfig) *OidcAuthConsumer {
  167. provider, err := oidc.NewProvider(context.Background(), cfg.OidcIssuer)
  168. if err != nil {
  169. panic(err)
  170. }
  171. verifierConf := oidc.Config{
  172. ClientID: cfg.OidcAudience,
  173. SkipClientIDCheck: cfg.OidcAudience == "",
  174. SkipExpiryCheck: cfg.OidcSkipExpiryCheck,
  175. SkipIssuerCheck: cfg.OidcSkipIssuerCheck,
  176. }
  177. return &OidcAuthConsumer{
  178. baseConfig: baseCfg,
  179. verifier: provider.Verifier(&verifierConf),
  180. }
  181. }
  182. func (auth *OidcAuthConsumer) VerifyLogin(loginMsg *msg.Login) (err error) {
  183. token, err := auth.verifier.Verify(context.Background(), loginMsg.PrivilegeKey)
  184. if err != nil {
  185. return fmt.Errorf("invalid OIDC token in login: %v", err)
  186. }
  187. auth.subjectFromLogin = token.Subject
  188. return nil
  189. }
  190. func (auth *OidcAuthConsumer) verifyPostLoginToken(privilegeKey string) (err error) {
  191. token, err := auth.verifier.Verify(context.Background(), privilegeKey)
  192. if err != nil {
  193. return fmt.Errorf("invalid OIDC token in ping: %v", err)
  194. }
  195. if token.Subject != auth.subjectFromLogin {
  196. return fmt.Errorf("received different OIDC subject in login and ping. "+
  197. "original subject: %s, "+
  198. "new subject: %s",
  199. auth.subjectFromLogin, token.Subject)
  200. }
  201. return nil
  202. }
  203. func (auth *OidcAuthConsumer) VerifyPing(pingMsg *msg.Ping) (err error) {
  204. if !auth.AuthenticateHeartBeats {
  205. return nil
  206. }
  207. return auth.verifyPostLoginToken(pingMsg.PrivilegeKey)
  208. }
  209. func (auth *OidcAuthConsumer) VerifyNewWorkConn(newWorkConnMsg *msg.NewWorkConn) (err error) {
  210. if !auth.AuthenticateNewWorkConns {
  211. return nil
  212. }
  213. return auth.verifyPostLoginToken(newWorkConnMsg.PrivilegeKey)
  214. }