2019-02-21 16:58:25 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/services/gateway/action"
|
|
|
|
"agola.io/agola/internal/util"
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OAuth2CallbackHandler struct {
|
2019-05-05 12:45:19 +00:00
|
|
|
log *zap.SugaredLogger
|
|
|
|
ah *action.ActionHandler
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RemoteSourceAuthResult struct {
|
|
|
|
RequestType string `json:"request_type,omitempty"`
|
|
|
|
Response interface{} `json:"response,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-05-05 12:45:19 +00:00
|
|
|
func NewOAuth2CallbackHandler(logger *zap.Logger, ah *action.ActionHandler) *OAuth2CallbackHandler {
|
|
|
|
return &OAuth2CallbackHandler{log: logger.Sugar(), ah: ah}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *OAuth2CallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
query := r.URL.Query()
|
|
|
|
code := query.Get("code")
|
|
|
|
state := query.Get("state")
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
cresp, err := h.ah.HandleOauth2Callback(ctx, code, state)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-04-09 12:53:00 +00:00
|
|
|
httpError(w, util.NewErrBadRequest(err))
|
2019-02-21 16:58:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var response interface{}
|
|
|
|
switch cresp.RequestType {
|
2019-05-03 21:48:49 +00:00
|
|
|
case action.RemoteSourceRequestTypeCreateUserLA:
|
|
|
|
authresp := cresp.Response.(*action.CreateUserLAResponse)
|
2019-02-21 16:58:25 +00:00
|
|
|
response = &CreateUserLAResponse{
|
|
|
|
LinkedAccount: authresp.LinkedAccount,
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
case action.RemoteSourceRequestTypeLoginUser:
|
|
|
|
authresp := cresp.Response.(*action.LoginUserResponse)
|
2019-02-21 16:58:25 +00:00
|
|
|
response = &LoginUserResponse{
|
|
|
|
Token: authresp.Token,
|
|
|
|
User: createUserResponse(authresp.User),
|
|
|
|
}
|
2019-03-29 16:53:15 +00:00
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
case action.RemoteSourceRequestTypeAuthorize:
|
|
|
|
authresp := cresp.Response.(*action.AuthorizeResponse)
|
2019-03-29 16:53:15 +00:00
|
|
|
response = &AuthorizeResponse{
|
|
|
|
RemoteUserInfo: authresp.RemoteUserInfo,
|
|
|
|
RemoteSourceName: authresp.RemoteSourceName,
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:48:49 +00:00
|
|
|
case action.RemoteSourceRequestTypeRegisterUser:
|
2019-03-29 16:53:15 +00:00
|
|
|
response = &RegisterUserResponse{}
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 09:35:45 +00:00
|
|
|
res := RemoteSourceAuthResult{
|
2019-03-29 16:53:15 +00:00
|
|
|
RequestType: string(cresp.RequestType),
|
2019-02-21 16:58:25 +00:00
|
|
|
Response: response,
|
|
|
|
}
|
2019-04-08 09:35:45 +00:00
|
|
|
if err := httpResponse(w, http.StatusOK, res); err != nil {
|
2019-04-05 14:33:00 +00:00
|
|
|
h.log.Errorf("err: %+v", err)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
}
|