You've already forked oauth2-proxy
mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2025-06-21 00:29:44 +02:00
Use upn claim as a fallback in Enrich & Refresh
Only when `email` claim is missing, fallback to `upn` claim which may have it.
This commit is contained in:
@ -2,6 +2,8 @@ package providers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@ -9,6 +11,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/coreos/go-oidc/v3/oidc"
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/ginkgo/extensions/table"
|
||||
@ -25,8 +28,18 @@ func (fakeADFSJwks) VerifySignature(_ context.Context, jwt string) (payload []by
|
||||
return decodeString, nil
|
||||
}
|
||||
|
||||
func testADFSProvider(hostname string) *ADFSProvider {
|
||||
type adfsClaims struct {
|
||||
UPN string `json:"upn,omitempty"`
|
||||
idTokenClaims
|
||||
}
|
||||
|
||||
func newSignedTestADFSToken(tokenClaims adfsClaims) (string, error) {
|
||||
key, _ := rsa.GenerateKey(rand.Reader, 2048)
|
||||
standardClaims := jwt.NewWithClaims(jwt.SigningMethodRS256, tokenClaims)
|
||||
return standardClaims.SignedString(key)
|
||||
}
|
||||
|
||||
func testADFSProvider(hostname string) *ADFSProvider {
|
||||
o := oidc.NewVerifier(
|
||||
"https://issuer.example.com",
|
||||
fakeADFSJwks{},
|
||||
@ -41,6 +54,7 @@ func testADFSProvider(hostname string) *ADFSProvider {
|
||||
ValidateURL: &url.URL{},
|
||||
Scope: "",
|
||||
Verifier: o,
|
||||
EmailClaim: OIDCEmailClaim,
|
||||
})
|
||||
|
||||
if hostname != "" {
|
||||
@ -54,7 +68,6 @@ func testADFSProvider(hostname string) *ADFSProvider {
|
||||
}
|
||||
|
||||
func testADFSBackend() *httptest.Server {
|
||||
|
||||
authResponse := `
|
||||
{
|
||||
"access_token": "my_access_token",
|
||||
@ -129,7 +142,6 @@ var _ = Describe("ADFS Provider Tests", func() {
|
||||
|
||||
Context("with valid token", func() {
|
||||
It("should not throw an error", func() {
|
||||
p.EmailClaim = "email"
|
||||
rawIDToken, _ := newSignedTestIDToken(defaultIDToken)
|
||||
idToken, err := p.Verifier.Verify(context.Background(), rawIDToken)
|
||||
Expect(err).To(BeNil())
|
||||
@ -202,4 +214,68 @@ var _ = Describe("ADFS Provider Tests", func() {
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
Context("UPN Fallback", func() {
|
||||
var idToken string
|
||||
var session *sessions.SessionState
|
||||
|
||||
BeforeEach(func() {
|
||||
var err error
|
||||
idToken, err = newSignedTestADFSToken(adfsClaims{
|
||||
UPN: "upn@company.com",
|
||||
idTokenClaims: minimalIDToken,
|
||||
})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
session = &sessions.SessionState{
|
||||
IDToken: idToken,
|
||||
}
|
||||
})
|
||||
|
||||
Describe("EnrichSession", func() {
|
||||
It("uses email claim if present", func() {
|
||||
p.oidcEnrichFunc = func(_ context.Context, s *sessions.SessionState) error {
|
||||
s.Email = "person@company.com"
|
||||
return nil
|
||||
}
|
||||
|
||||
err := p.EnrichSession(context.Background(), session)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(session.Email).To(Equal("person@company.com"))
|
||||
})
|
||||
|
||||
It("falls back to UPN claim if Email is missing", func() {
|
||||
p.oidcEnrichFunc = func(_ context.Context, s *sessions.SessionState) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := p.EnrichSession(context.Background(), session)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(session.Email).To(Equal("upn@company.com"))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("RefreshSession", func() {
|
||||
It("uses email claim if present", func() {
|
||||
p.oidcRefreshFunc = func(_ context.Context, s *sessions.SessionState) (bool, error) {
|
||||
s.Email = "person@company.com"
|
||||
return true, nil
|
||||
}
|
||||
|
||||
_, err := p.RefreshSession(context.Background(), session)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(session.Email).To(Equal("person@company.com"))
|
||||
})
|
||||
|
||||
It("falls back to UPN claim if Email is missing", func() {
|
||||
p.oidcRefreshFunc = func(_ context.Context, s *sessions.SessionState) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
_, err := p.RefreshSession(context.Background(), session)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(session.Email).To(Equal("upn@company.com"))
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user