Skip to content

Commit a03210d

Browse files
authored
Merge pull request #328 from Shuffle/0x0elliot/sso-revert
fix: reverting once more
2 parents 3f7a56c + c7ead1c commit a03210d

File tree

3 files changed

+224
-629
lines changed

3 files changed

+224
-629
lines changed

oauth2.go

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"strings"
2323
"time"
2424

25-
"github.com/coreos/go-oidc/v3/oidc"
2625
"github.com/google/go-querystring/query"
2726
uuid "github.com/satori/go.uuid"
2827
"golang.org/x/oauth2"
@@ -39,6 +38,7 @@ import (
3938

4039
var handledIds []string
4140

41+
/*
4242
func fetchUserInfoFromToken(ctx context.Context, accessToken string, issuer string, openIdAuthUrl string) (map[string]interface{}, error) {
4343
// Get well-known config to find userinfo endpoint
4444
config, err := fetchWellKnownConfig(ctx, issuer, openIdAuthUrl)
@@ -102,6 +102,7 @@ func fetchUserInfoFromToken(ctx context.Context, accessToken string, issuer stri
102102
103103
return userInfo, nil
104104
}
105+
*/
105106

106107
func GetOutlookAttachmentList(client *http.Client, emailId string) (MailDataOutlookList, error) {
107108
requestUrl := fmt.Sprintf("https://graph.microsoft.com/v1.0/me/messages/%s/attachments", emailId)
@@ -4199,6 +4200,7 @@ func RunOauth2Request(ctx context.Context, user User, appAuth AppAuthenticationS
41994200
return appAuth, nil
42004201
}
42014202

4203+
/*
42024204
func fetchWellKnownConfig(ctx context.Context, issuer string, openIdAuthUrl string) (map[string]interface{}, error) {
42034205
// Clean issuer URL and construct well-known endpoint
42044206
issuer = strings.TrimSuffix(issuer, "/")
@@ -4306,78 +4308,55 @@ func ExtractRolesFromIdToken(ctx context.Context, idToken string, issuer string,
43064308
43074309
return roles, nil
43084310
}
4309-
4310-
func VerifyIdToken(ctx context.Context, idToken string, accessToken string) (IdTokenCheck, string, error) {
4311-
var emptyToken IdTokenCheck
4312-
var foundChallenge string
4313-
4314-
// Parse JWT properly with all three parts
4315-
outerSplit := strings.Split(idToken, ".")
4316-
if len(outerSplit) != 3 {
4317-
return emptyToken, "", fmt.Errorf("invalid JWT format")
4318-
}
4319-
4320-
// Try to decode the payload (middle part)
4321-
for idx, innerstate := range outerSplit {
4322-
// Skip header (0) and signature (2), focus on payload (1)
4323-
if idx != 1 {
4324-
continue
4325-
}
4326-
4327-
// Use RawURLEncoding for JWT (no padding)
4328-
decoded, err := base64.RawURLEncoding.DecodeString(innerstate)
4311+
*/
4312+
4313+
func VerifyIdToken(ctx context.Context, idToken string) (IdTokenCheck, error) {
4314+
// Check org in nonce -> check if ID points back to an org
4315+
outerSplit := strings.Split(string(idToken), ".")
4316+
for _, innerstate := range outerSplit {
4317+
log.Printf("[DEBUG] OpenID STATE (temporary): %s", innerstate)
4318+
decoded, err := base64.StdEncoding.DecodeString(innerstate)
43294319
if err != nil {
4330-
log.Printf("[DEBUG] RawURLEncoding failed, trying with padding: %s", err)
4331-
// Try URL encoding with padding
4332-
decoded, err = base64.URLEncoding.DecodeString(innerstate)
4320+
log.Printf("[DEBUG] Failed base64 decode of state (1): %s", err)
4321+
4322+
// Random padding problems
4323+
innerstate += "="
4324+
decoded, err = base64.StdEncoding.DecodeString(innerstate)
43334325
if err != nil {
4334-
// Try with extra padding
4326+
log.Printf("[DEBUG] Failed base64 decode of state (2): %s", err)
4327+
4328+
// Double padding problem fix lol (this actually works)
43354329
innerstate += "="
4336-
decoded, err = base64.URLEncoding.DecodeString(innerstate)
4330+
decoded, err = base64.StdEncoding.DecodeString(innerstate)
43374331
if err != nil {
4338-
innerstate += "="
4339-
decoded, err = base64.URLEncoding.DecodeString(innerstate)
4340-
if err != nil {
4341-
preview := innerstate
4342-
if len(preview) > 50 {
4343-
preview = preview[:50] + "..."
4344-
}
4345-
log.Printf("[ERROR] Failed to decode JWT payload: %s (innerstate preview: %s)", err, preview)
4346-
return emptyToken, "", fmt.Errorf("failed to decode JWT payload")
4347-
}
4332+
log.Printf("[ERROR] Failed base64 decode of state (3): %s", err)
4333+
continue
43484334
}
43494335
}
43504336
}
43514337

43524338
var token IdTokenCheck
4353-
err = json.Unmarshal(decoded, &token)
4339+
err = json.Unmarshal([]byte(decoded), &token)
43544340
if err != nil {
4355-
log.Printf("[ERROR] Failed to unmarshal JWT payload: %s", err)
4356-
return emptyToken, "", fmt.Errorf("failed to unmarshal JWT payload")
4357-
}
4358-
4359-
// Validate required fields
4360-
if len(token.Aud) == 0 {
4361-
return emptyToken, "", fmt.Errorf("missing audience in token")
4341+
log.Printf("[INFO] IDToken unmarshal error: %s", err)
4342+
continue
43624343
}
43634344

4364-
// Check token expiration
4365-
if token.Exp > 0 {
4366-
now := time.Now().Unix()
4367-
if now >= int64(token.Exp) {
4368-
log.Printf("[ERROR] JWT token expired: exp=%d, now=%d", token.Exp, now)
4369-
return emptyToken, "", fmt.Errorf("JWT token expired")
4370-
}
4345+
// Aud = client secret
4346+
// Nonce = contains all the info
4347+
if len(token.Aud) <= 0 {
4348+
log.Printf("[WARNING] Couldn't find AUD in JSON (required) - continuing to check. Current: %s", string(decoded))
4349+
continue
43714350
}
43724351

4373-
// Verify JWT signature if we have an issuer
43744352
if len(token.Nonce) > 0 {
43754353
parsedState, err := base64.StdEncoding.DecodeString(token.Nonce)
43764354
if err != nil {
43774355
log.Printf("[ERROR] Failed state split: %s", err)
43784356
}
43794357

43804358
foundOrg := ""
4359+
foundChallenge := ""
43814360
stateSplit := strings.Split(string(parsedState), "&")
43824361
regexPattern := `EXTRA string=([A-Za-z0-9~.]+)`
43834362
re := regexp.MustCompile(regexPattern)
@@ -4410,23 +4389,24 @@ func VerifyIdToken(ctx context.Context, idToken string, accessToken string) (IdT
44104389

44114390
if len(foundOrg) == 0 {
44124391
log.Printf("[ERROR] No org specified in state (2)")
4413-
return IdTokenCheck{}, foundChallenge, err
4392+
return IdTokenCheck{}, err
44144393
}
44154394
org, err := GetOrg(ctx, foundOrg)
44164395
if err != nil {
44174396
log.Printf("[WARNING] Error getting org in OpenID (2): %s", err)
4418-
return IdTokenCheck{}, foundChallenge, err
4397+
return IdTokenCheck{}, err
44194398
}
44204399
// Validating the user itself
44214400
if token.Aud == org.SSOConfig.OpenIdClientId || foundChallenge == org.SSOConfig.OpenIdClientSecret {
44224401
log.Printf("[DEBUG] Correct token aud & challenge - successful login!")
44234402
token.Org = *org
4424-
return token, foundChallenge, nil
4403+
return token, nil
4404+
} else {
44254405
}
44264406
}
44274407
}
44284408

4429-
return IdTokenCheck{}, "", errors.New("Couldn't verify nonce")
4409+
return IdTokenCheck{}, errors.New("Couldn't verify nonce")
44304410
}
44314411

44324412
func IsRunningInCluster() bool {

0 commit comments

Comments
 (0)