| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2019-10-23 16:40:12 +08:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-27 00:23:28 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							| 
									
										
										
										
											2015-02-27 00:23:28 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/apikeygen" | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | func GetAPIKeys(c *models.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2019-11-20 19:14:57 +08:00
										 |  |  | 	query := models.GetApiKeysQuery{OrgId: c.OrgId, IncludeExpired: c.QueryBool("includeExpired")} | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to list api keys", err) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 	result := make([]*models.ApiKeyDTO, len(query.Result)) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 	for i, t := range query.Result { | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 		var expiration *time.Time = nil | 
					
						
							|  |  |  | 		if t.Expires != nil { | 
					
						
							|  |  |  | 			v := time.Unix(*t.Expires, 0) | 
					
						
							|  |  |  | 			expiration = &v | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		result[i] = &models.ApiKeyDTO{ | 
					
						
							|  |  |  | 			Id:         t.Id, | 
					
						
							|  |  |  | 			Name:       t.Name, | 
					
						
							|  |  |  | 			Role:       t.Role, | 
					
						
							|  |  |  | 			Expiration: expiration, | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-05-19 03:23:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, result) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | func DeleteAPIKey(c *models.ReqContext) Response { | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 	id := c.ParamsInt64(":id") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 	cmd := &models.DeleteApiKeyCommand{Id: id, OrgId: c.OrgId} | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	err := bus.Dispatch(cmd) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(500, "Failed to delete API key", err) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return Success("API key deleted") | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | func (hs *HTTPServer) AddAPIKey(c *models.ReqContext, cmd models.AddApiKeyCommand) Response { | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 	if !cmd.Role.IsValid() { | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 		return Error(400, "Invalid role specified", nil) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 	if hs.Cfg.ApiKeyMaxSecondsToLive != -1 { | 
					
						
							|  |  |  | 		if cmd.SecondsToLive == 0 { | 
					
						
							|  |  |  | 			return Error(400, "Number of seconds before expiration should be set", nil) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if cmd.SecondsToLive > hs.Cfg.ApiKeyMaxSecondsToLive { | 
					
						
							|  |  |  | 			return Error(400, "Number of seconds before expiration is greater than the global limit", nil) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	cmd.OrgId = c.OrgId | 
					
						
							| 
									
										
										
										
											2015-02-27 00:23:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-23 16:40:12 +08:00
										 |  |  | 	newKeyInfo, err := apikeygen.New(cmd.OrgId, cmd.Name) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return Error(500, "Generating API key failed", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-27 00:23:28 +08:00
										 |  |  | 	cmd.Key = newKeyInfo.HashedKey | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2019-06-26 14:47:03 +08:00
										 |  |  | 		if err == models.ErrInvalidApiKeyExpiration { | 
					
						
							|  |  |  | 			return Error(400, err.Error(), nil) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-11 16:20:34 +08:00
										 |  |  | 		if err == models.ErrDuplicateApiKey { | 
					
						
							|  |  |  | 			return Error(409, err.Error(), nil) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return Error(500, "Failed to add API Key", err) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-27 00:23:28 +08:00
										 |  |  | 	result := &dtos.NewApiKeyResult{ | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 		Name: cmd.Result.Name, | 
					
						
							| 
									
										
										
										
											2015-05-19 03:23:40 +08:00
										 |  |  | 		Key:  newKeyInfo.ClientSecret} | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-23 05:13:46 +08:00
										 |  |  | 	return JSON(200, result) | 
					
						
							| 
									
										
										
										
											2015-01-27 15:26:11 +08:00
										 |  |  | } |