Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1128 - Print the entropy to stderr regardless of quiet mode #1129

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/provider/aad/aad.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"net/http"
"net/url"
"os"
"strings"
"time"

Expand Down Expand Up @@ -465,7 +466,10 @@ func (ac *Client) processMfa(mfas []userProof, convergedResponse *ConvergedRespo
if mfaResp.Entropy == 0 {
log.Println("Phone approval required.")
} else {
prevWriter := log.Writer()
log.SetOutput(os.Stderr)
log.Printf("Phone approval required. Entropy is: %d", mfaResp.Entropy)
log.SetOutput(prevWriter)
}
}

Expand Down
74 changes: 74 additions & 0 deletions pkg/provider/aad/aad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"math"
mrand "math/rand"
"net/http"
Expand Down Expand Up @@ -62,6 +63,8 @@ type FixtureData struct {
UserName string // [email protected]
UserNameUrlEncoded string // exampleuser%40exampledomain.com
SErrorCode string // 50058
AuthMethodId string // OneWaySMS
Entropy string // 0
}

var fixtureData *FixtureData
Expand Down Expand Up @@ -89,6 +92,8 @@ func genFixtureData() *FixtureData {
ProofUpToken: genBase64Fixture(400),
UserName: "[email protected]",
UserNameUrlEncoded: "exampleuser%40exampledomain.com",
AuthMethodId: "OneWaySMS",
Entropy: "0",
}
})
return fixtureData
Expand Down Expand Up @@ -360,6 +365,69 @@ func Test_Authenticate(t *testing.T) {
require.Error(t, err)
require.Contains(t, err.Error(), "502031")
})
t.Run("Default login with KMSI and MFA with entropy", func(t *testing.T) {
entropy := genIntFixture(2)
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/index", "/applications/redirecttofederatedapplication.aspx":
writeFixtureBytes(t, w, r, "ConvergedSignIn.html", FixtureData{
UrlPost: "/defaultLogin",
UrlGetCredentialType: "/getCredentialType",
})
case "/getCredentialType":
writeFixtureBytes(t, w, r, "GetCredentialType_default.json", FixtureData{})
case "/defaultLogin":
writeFixtureBytes(t, w, r, "KmsiInterrupt.html", FixtureData{
UrlPost: "/hForm",
})
case "/hForm":
writeFixtureBytes(t, w, r, "HiddenForm.html", FixtureData{
UrlHiddenForm: "/sRequest",
})
case "/sRequest":
writeFixtureBytes(t, w, r, "SAMLRequest.html", FixtureData{
UrlSamlRequest: "/sResponse?SAMLRequest=ExampleValue",
})
case "/sResponse":
writeFixtureBytes(t, w, r, "ConvergedTFA.html", FixtureData{
UrlPost: "/processAuth",
UrlBeginAuth: "/beginAuth",
UrlEndAuth: "/endAuth",
})
case "/beginAuth":
writeFixtureBytes(t, w, r, "BeginAuth.json", FixtureData{
AuthMethodId: "PhoneAppNotification",
Entropy: entropy,
})
case "/endAuth":
writeFixtureBytes(t, w, r, "EndAuth.json", FixtureData{
AuthMethodId: "PhoneAppNotification",
Entropy: entropy,
})
case "/processAuth":
writeFixtureBytes(t, w, r, "SAMLResponse.html", FixtureData{})
default:
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
}))
defer ts.Close()

ac, loginDetails := setupTestClient(t, ts)

orig := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w

got, err := ac.Authenticate(loginDetails)

os.Stderr = orig
w.Close()
out, _ := io.ReadAll(r)

require.Contains(t, string(out), "Phone approval required. Entropy is: "+entropy)
require.Nil(t, err)
require.NotEmpty(t, got)
})
t.Run("ADFS login with KMSI and MFA", func(t *testing.T) {
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
Expand Down Expand Up @@ -492,6 +560,12 @@ func responseFixtures(host string, variableFixture FixtureData) *FixtureData {
} else {
fixtureData.UrlSamlRequest = ""
}
if variableFixture.AuthMethodId != "" {
fixtureData.AuthMethodId = variableFixture.AuthMethodId
}
if variableFixture.Entropy != "" {
fixtureData.Entropy = variableFixture.Entropy
}
return fixtureData
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/aad/testdata/BeginAuth.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"Success":true,"ResultValue":"Success","Message":null,"AuthMethodId":"OneWaySMS","ErrCode":0,"Retry":false,"FlowToken":"{{.SFT}}","Ctx":"{{.Ctx}}","SessionId":"{{.SessionId}}","CorrelationId":"{{.ClientRequestId}}","Timestamp":"2020-01-01T00:00:00Z","Entropy":0}
{"Success":true,"ResultValue":"Success","Message":null,"AuthMethodId":"{{.AuthMethodId}}","ErrCode":0,"Retry":false,"FlowToken":"{{.SFT}}","Ctx":"{{.Ctx}}","SessionId":"{{.SessionId}}","CorrelationId":"{{.ClientRequestId}}","Timestamp":"2020-01-01T00:00:00Z","Entropy":{{.Entropy}}}
2 changes: 1 addition & 1 deletion pkg/provider/aad/testdata/EndAuth.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"Success":true,"ResultValue":"Success","Message":null,"AuthMethodId":"OneWaySMS","ErrCode":0,"Retry":false,"FlowToken":"{{.SFT}}","Ctx":"{{.Ctx}}","SessionId":"{{.SessionId}}","CorrelationId":"{{.ClientRequestId}}","Timestamp":"2020-01-01T00:00:00Z","Entropy":0}
{"Success":true,"ResultValue":"Success","Message":null,"AuthMethodId":"{{.AuthMethodId}}","ErrCode":0,"Retry":false,"FlowToken":"{{.SFT}}","Ctx":"{{.Ctx}}","SessionId":"{{.SessionId}}","CorrelationId":"{{.ClientRequestId}}","Timestamp":"2020-01-01T00:00:00Z","Entropy":{{.Entropy}}}