Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created September 25, 2022 11:45
Show Gist options
  • Save percybolmer/a6ecf4102d4dd83e387dad5fed0b30e2 to your computer and use it in GitHub Desktop.
Save percybolmer/a6ecf4102d4dd83e387dad5fed0b30e2 to your computer and use it in GitHub Desktop.
// loginHandler is used to verify an user authentication and return a one time password
func (m *Manager) loginHandler(w http.ResponseWriter, r *http.Request) {
type userLoginRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}
var req userLoginRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Authenticate user / Verify Access token, what ever auth method you use
if req.Username == "percy" && req.Password == "123" {
// format to return otp in to the frontend
type response struct {
OTP string `json:"otp"`
}
// add a new OTP
otp := m.otps.NewOTP()
resp := response{
OTP: otp.Key,
}
data, err := json.Marshal(resp)
if err != nil {
log.Println(err)
return
}
// Return a response to the Authenticated user with the OTP
w.WriteHeader(http.StatusOK)
w.Write(data)
return
}
// Failure to auth
w.WriteHeader(http.StatusUnauthorized)
}
// serveWS is a HTTP Handler that the has the Manager that allows connections
func (m *Manager) serveWS(w http.ResponseWriter, r *http.Request) {
// Grab the OTP in the Get param
otp := r.URL.Query().Get("otp")
if otp == "" {
// Tell the user its not authorized
w.WriteHeader(http.StatusUnauthorized)
return
}
// Verify OTP is existing
if !m.otps.VerifyOTP(otp) {
w.WriteHeader(http.StatusUnauthorized)
return
}
log.Println("New connection")
// Begin by upgrading the HTTP request
conn, err := websocketUpgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
// Create New Client
client := NewClient(conn, m)
// Add the newly created client to the manager
m.addClient(client)
go client.readMessages()
go client.writeMessages()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment