Files
+14 4a963a2e1f [MM-53968] Includes mattermost-plugin-api into the mono repo (#24235)
Include https://github.com/mattermost/mattermost-plugin-api into the mono repo

Co-authored-by: Jesse Hallam <[email protected]>
Co-authored-by: Michael Kochell <[email protected]>
Co-authored-by: Alejandro García Montoro <[email protected]>
Co-authored-by: Ben Schumacher <[email protected]>
Co-authored-by: Alex Dovenmuehle <[email protected]>
Co-authored-by: Michael Kochell <[email protected]>
Co-authored-by: Christopher Poile <[email protected]>
Co-authored-by: İlker Göktuğ Öztürk <[email protected]>
Co-authored-by: Shota Gvinepadze <[email protected]>
Co-authored-by: Ali Farooq <[email protected]>
Co-authored-by: Maria A Nunez <[email protected]>
Co-authored-by: Daniel Espino García <[email protected]>
Co-authored-by: Christopher Speller <[email protected]>
Co-authored-by: Alex Dovenmuehle <[email protected]>
Co-authored-by: Szymon Gibała <[email protected]>
Co-authored-by: Lev <[email protected]>
Co-authored-by: Jason Frerich <[email protected]>
Co-authored-by: Agniva De Sarker <[email protected]>
Co-authored-by: Artur M. Wolff <[email protected]>
Co-authored-by: Madhav Hugar <[email protected]>
Co-authored-by: Joe <[email protected]>
Co-authored-by: Ibrahim Serdar Acikgoz <[email protected]>
Co-authored-by: José Peso <[email protected]>
2023-08-21 09:50:30 +02:00

106 lines
2.8 KiB
Go

// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved.
// See License for license information.
package oauther
import (
"context"
"fmt"
"net/http"
"strings"
)
func (o *oAuther) oauth2Complete(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
authedUserID := r.Header.Get("Mattermost-User-ID")
if authedUserID == "" {
o.logger.Debugf("oauth2Complete: reached by non authed user")
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
code := r.URL.Query().Get("code")
if code == "" {
o.logger.Debugf("oauth2Complete: reached with no code")
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
state := r.URL.Query().Get("state")
var storedState string
err := o.store.Get(o.getStateKey(authedUserID), &storedState)
if err != nil {
o.logger.Warnf("oauth2Complete: cannot get state, err=%s", err.Error())
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
if storedState != state {
o.logger.Debugf("oauth2Complete: state mismatch")
o.logger.Debugf("received state '%s'; expected state '%s%", state, storedState)
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
userID := strings.Split(state, "_")[1]
if userID != authedUserID {
o.logger.Debugf("oauth2Complete: authed user mismatch")
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
ctx := context.Background()
token, err := o.config.Exchange(ctx, code)
if err != nil {
o.logger.Warnf("oauth2Complete: could not generate token, err=%s", err.Error())
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
var payload []byte
err = o.store.Get(o.getPayloadKey(userID), &payload)
if err != nil {
o.logger.Errorf("oauth2Complete: could not fetch payload, err=&s", err.Error())
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
ok, err := o.store.Set(o.getTokenKey(userID), token)
if err != nil {
o.logger.Errorf("oauth2Complete: cannot store the token, err=%s", err.Error())
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
if !ok {
o.logger.Errorf("oauth2Complete: cannot store token without error")
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
html := fmt.Sprintf(`
<!DOCTYPE html>
<html>
<head>
<script>
window.close();
</script>
</head>
<body>
<p>%s</p>
</body>
</html>
`, o.connectedString)
w.Header().Set("Content-Type", "text/html")
_, err = w.Write([]byte(html))
if err != nil {
o.logger.Errorf("oauth2Complete: error writing response, err=%s", err.Error())
}
if o.onConnect != nil {
o.onConnect(userID, *token, payload)
}
}