| 
									
										
										
										
											2020-11-18 22:36:41 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-14 20:11:26 +08:00
										 |  |  | import ( | 
					
						
							| 
									
										
										
										
											2024-04-17 21:24:36 +08:00
										 |  |  | 	"context" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2022-11-14 20:11:26 +08:00
										 |  |  | 	"net/mail" | 
					
						
							| 
									
										
										
										
											2023-03-23 21:39:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-17 21:24:36 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/response" | 
					
						
							| 
									
										
										
										
											2023-03-23 21:39:04 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/middleware/cookies" | 
					
						
							|  |  |  | 	contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" | 
					
						
							| 
									
										
										
										
											2024-04-17 21:24:36 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/login" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/user" | 
					
						
							| 
									
										
										
										
											2024-08-06 09:17:39 +08:00
										 |  |  | 	"go.opentelemetry.io/otel/trace" | 
					
						
							| 
									
										
										
										
											2022-11-14 20:11:26 +08:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2020-11-18 22:36:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-23 21:39:04 +08:00
										 |  |  | func (hs *HTTPServer) GetRedirectURL(c *contextmodel.ReqContext) string { | 
					
						
							|  |  |  | 	redirectURL := hs.Cfg.AppSubURL + "/" | 
					
						
							|  |  |  | 	if redirectTo := c.GetCookie("redirect_to"); len(redirectTo) > 0 { | 
					
						
							|  |  |  | 		if err := hs.ValidateRedirectTo(redirectTo); err == nil { | 
					
						
							|  |  |  | 			redirectURL = redirectTo | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			hs.log.FromContext(c.Req.Context()).Debug("Ignored invalid redirect_to cookie value", "redirect_to", redirectTo) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		cookies.DeleteCookie(c.Resp, "redirect_to", hs.CookieOptionsFromCfg) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return redirectURL | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-17 21:24:36 +08:00
										 |  |  | func (hs *HTTPServer) errOnExternalUser(ctx context.Context, userID int64) response.Response { | 
					
						
							|  |  |  | 	isExternal, err := hs.isExternalUser(ctx, userID) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusInternalServerError, "Failed to validate User", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if isExternal { | 
					
						
							|  |  |  | 		return response.Error(http.StatusForbidden, "Cannot update external User", nil) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (hs *HTTPServer) isExternalUser(ctx context.Context, userID int64) (bool, error) { | 
					
						
							|  |  |  | 	info, err := hs.authInfoService.GetAuthInfo(ctx, &login.GetAuthInfoQuery{UserId: userID}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if errors.Is(err, user.ErrUserNotFound) { | 
					
						
							|  |  |  | 		return false, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return true, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return login.IsProviderEnabled(hs.Cfg, info.AuthModule, hs.SocialService.GetOAuthInfoProvider(info.AuthModule)), nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-14 20:11:26 +08:00
										 |  |  | func ValidateAndNormalizeEmail(email string) (string, error) { | 
					
						
							|  |  |  | 	if email == "" { | 
					
						
							|  |  |  | 		return "", nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	e, err := mail.ParseAddress(email) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return e.Address, nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-08-06 09:17:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (hs *HTTPServer) injectSpan(c *contextmodel.ReqContext, name string) (*contextmodel.ReqContext, trace.Span) { | 
					
						
							|  |  |  | 	ctx, span := hs.tracer.Start(c.Req.Context(), name) | 
					
						
							|  |  |  | 	c.Req = c.Req.WithContext(ctx) | 
					
						
							|  |  |  | 	return c, span | 
					
						
							|  |  |  | } |