| 
									
										
										
										
											2020-11-18 22:36:41 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-14 20:11:26 +08:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"net/mail" | 
					
						
							| 
									
										
										
										
											2023-03-23 21:39:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/middleware/cookies" | 
					
						
							|  |  |  | 	contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | } |