38 lines
1.1 KiB
Go
38 lines
1.1 KiB
Go
package config
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
v1 "likedream-goframe/internal/api/v1"
|
|
mailservice "likedream-goframe/internal/service/email"
|
|
"likedream-goframe/internal/utils"
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
)
|
|
|
|
type MailOAuthCtrlV1 struct{}
|
|
|
|
func NewMailOAuthCtrlV1() *MailOAuthCtrlV1 {
|
|
return &MailOAuthCtrlV1{}
|
|
}
|
|
|
|
func (c *MailOAuthCtrlV1) OutlookCallback(ctx context.Context, req *v1.OutlookOAuthCallbackReq) (res *utils.Response[any], err error) {
|
|
r := ghttp.RequestFromCtx(ctx)
|
|
r.Response.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
if req.Error != "" {
|
|
r.Response.Write(mailservice.BuildOutlookOAuthErrorHTML(fmt.Sprintf("%s: %s", req.Error, req.ErrorDescription)))
|
|
return nil, nil
|
|
}
|
|
|
|
refreshToken, frontendOrigin, exchangeErr := mailservice.NewV1().ExchangeOutlookAuthorizeCode(ctx, req.State, req.Code)
|
|
if exchangeErr != nil {
|
|
r.Response.Write(mailservice.BuildOutlookOAuthPopupHTML("*", false, exchangeErr.Error(), ""))
|
|
return nil, nil
|
|
}
|
|
|
|
r.Response.Write(mailservice.BuildOutlookOAuthPopupHTML(frontendOrigin, true, "授权成功", refreshToken))
|
|
return nil, nil
|
|
}
|