| 131 | } |
| 132 | |
| 133 | func (h oauthHandler) viewOauthInit(app *App, w http.ResponseWriter, r *http.Request) error { |
| 134 | ctx := r.Context() |
| 135 | |
| 136 | var attachUser int64 |
| 137 | if attach := r.URL.Query().Get("attach"); attach == "t" { |
| 138 | user, _ := getUserAndSession(app, r) |
| 139 | if user == nil { |
| 140 | return impart.HTTPError{http.StatusInternalServerError, "cannot attach auth to user: user not found in session"} |
| 141 | } |
| 142 | attachUser = user.ID |
| 143 | } |
| 144 | |
| 145 | state, err := h.DB.GenerateOAuthState(ctx, h.oauthClient.GetProvider(), h.oauthClient.GetClientID(), attachUser, r.FormValue("invite_code")) |
| 146 | if err != nil { |
| 147 | log.Error("viewOauthInit error: %s", err) |
| 148 | return impart.HTTPError{http.StatusInternalServerError, "could not prepare oauth redirect url"} |
| 149 | } |
| 150 | |
| 151 | if h.callbackProxy != nil { |
| 152 | if err := h.callbackProxy.register(ctx, state); err != nil { |
| 153 | log.Error("viewOauthInit error: %s", err) |
| 154 | return impart.HTTPError{http.StatusInternalServerError, "could not register state server"} |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | location, err := h.oauthClient.buildLoginURL(state) |
| 159 | if err != nil { |
| 160 | log.Error("viewOauthInit error: %s", err) |
| 161 | return impart.HTTPError{http.StatusInternalServerError, "could not prepare oauth redirect url"} |
| 162 | } |
| 163 | return impart.HTTPError{http.StatusTemporaryRedirect, location} |
| 164 | } |
| 165 | |
| 166 | func configureSlackOauth(parentHandler *Handler, r *mux.Router, app *App) { |
| 167 | if app.Config().SlackOauth.ClientID != "" { |