|
package mockslack |
|
|
|
import ( |
|
"context" |
|
"net/http" |
|
) |
|
|
|
|
|
type OAuthAccessOpts struct { |
|
ClientID string |
|
ClientSecret string |
|
Code string |
|
} |
|
|
|
|
|
func (st *API) OAuthAccess(ctx context.Context, opts OAuthAccessOpts) (*AuthToken, error) { |
|
st.mx.Lock() |
|
defer st.mx.Unlock() |
|
|
|
app := st.apps[opts.ClientID] |
|
if app == nil { |
|
return nil, &response{Err: "invalid_client_id"} |
|
} |
|
|
|
if app.Secret != opts.ClientSecret { |
|
return nil, &response{Err: "bad_client_secret"} |
|
} |
|
|
|
tok := st.tokenCodes[opts.Code] |
|
if tok == nil || tok.ClientID != opts.ClientID { |
|
return nil, &response{Err: "invalid_code"} |
|
} |
|
|
|
delete(st.tokenCodes, opts.Code) |
|
|
|
return tok.AuthToken, nil |
|
} |
|
|
|
|
|
|
|
|
|
func (s *Server) ServeOAuthAccess(w http.ResponseWriter, req *http.Request) { |
|
usr, pass, _ := req.BasicAuth() |
|
tok, err := s.API().OAuthAccess(req.Context(), OAuthAccessOpts{ClientID: usr, ClientSecret: pass, Code: req.FormValue("code")}) |
|
if respondErr(w, err) { |
|
return |
|
} |
|
|
|
var resp struct { |
|
AccessToken string `json:"access_token"` |
|
UserID string `json:"user_id"` |
|
} |
|
resp.AccessToken = tok.ID |
|
resp.UserID = tok.User |
|
|
|
respondWith(w, resp) |
|
} |
|
|