Skip to content
This repository has been archived by the owner on Dec 14, 2020. It is now read-only.

Implement SASL OAuth2 implementation #193

Merged
merged 6 commits into from
Dec 5, 2019
Merged

Implement SASL OAuth2 implementation #193

merged 6 commits into from
Dec 5, 2019

Conversation

k-wall
Copy link
Contributor

@k-wall k-wall commented Nov 25, 2019

This patch add supports for a SASL OAuth2 mechanism.

I had some problems implementing tests for the new mechanism. The patch currently uses the testconn but I'm uncertain that using this package from the unit tests correct. I'll appreciate feedback on the test approach/implementation.

@vcabbage
Copy link
Owner

testconn is currently only used for fuzz testing. Feel free to use it if it works for these. As you probably noticed, there are no unit tests for the other SASL mechanisms. Only PLAIN is tested via the integration tests.

@k-wall
Copy link
Contributor Author

k-wall commented Nov 26, 2019

testconn is currently only used for fuzz testing. Feel free to use it if it works for these.

Ok, fine. I'll push the review button on this PR.

As you probably noticed, there are no unit tests for the other SASL mechanisms. Only PLAIN is tested via the integration tests.

XOAUTH2 has two possible flows for the error cases, the second one involving the sasl-challenge frame. Having unit test supporting both was important.

@k-wall k-wall marked this pull request as ready for review November 26, 2019 08:25
sasl.go Show resolved Hide resolved
sasl.go Outdated
}

func saslXOAUTH2InitialResponse(username string, bearer string) ([]byte, error) {
re := regexp.MustCompile("^[\x20-\x7E]+$")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a loop instead of regex for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

sasl.go Outdated Show resolved Hide resolved
sasl_test.go Outdated Show resolved Hide resolved
sasl_test.go Show resolved Hide resolved
sasl_test.go Outdated Show resolved Hide resolved
sasl_test.go Outdated Show resolved Hide resolved
sasl.go Show resolved Hide resolved
sasl.go Show resolved Hide resolved
Copy link
Owner

@vcabbage vcabbage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making those changes. Left a few more minor ones. Should be good to merge after they're resolved.

response: response,
}
return handler.init()
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The anonymous function can be omitted by constructing handler before the map assignment and setting c.saslHandlers[saslMechanismXOAUTH2] = handler.init.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, much neater. Done.

sasl.go Outdated
return s.saslXOAUTH2Step
}

func (s saslXOAUTH2Handler) saslXOAUTH2Step() stateFunc {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the receiver type includes "saslXOAUTH2" I think it'd be less repetitive to name the method step.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

sasl.go Outdated
Response: []byte{},
},
})
return s.saslXOAUTH2Step
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding a check that multiple challenges aren't received? Hypothetically this could loop for as long as the peer sends them.

Copy link
Contributor Author

@k-wall k-wall Dec 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added an additional guard. I now fail the negotiation on receipt of the second challenge from the server, including the contents of both error responses in the connection's error.

Saving the error response as a field in the receiver type had the additional benefit I could include it in the "SASL XOAUTH2 auth failed" error produced when the authentication fails.

sasl_test.go Outdated
client, err := New(c,
ConnSASLXOAUTH2("someuser@example.com", "ya29.vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEuY29tCg", 512),
ConnIdleTimeout(10*time.Minute))
if client != nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go tends to assume that if err != nil the other return values are invalid regardless of whether they're nil or not (unless documented otherwise). Can you change this to if err != nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

sasl_test.go Outdated
client, err := New(c,
ConnSASLXOAUTH2("someuser@example.com", "ya29.vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEuY29tCg", 512),
ConnIdleTimeout(10*time.Minute))
if client != nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@k-wall
Copy link
Contributor Author

k-wall commented Dec 5, 2019

@vcabbage thanks for the feedback, hopefully I've addressed your comments above.

@k-wall k-wall requested a review from vcabbage December 5, 2019 10:55
@vcabbage vcabbage merged commit 5a75e78 into vcabbage:master Dec 5, 2019
@vcabbage
Copy link
Owner

vcabbage commented Dec 5, 2019

Thank you.

Unfortunately this is going to be one of the last PRs to be merged. Please see #205 for details. Thank you again for your contributions.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants