2018-05-17 09:35:00 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2018-05-17 09:35:00 +05:30
|
|
|
|
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
2021-04-05 21:00:52 +05:30
|
|
|
"net/http"
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
2018-05-17 09:35:00 +05:30
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-04-07 01:14:05 +05:30
|
|
|
"code.gitea.io/gitea/services/forms"
|
2018-05-17 09:35:00 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tplSettingsApplications base.TplName = "user/settings/applications"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Applications render manage access token page
|
|
|
|
func Applications(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsApplications"] = true
|
|
|
|
|
2018-06-18 23:54:45 +05:30
|
|
|
loadApplicationsData(ctx)
|
2018-05-17 09:35:00 +05:30
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplSettingsApplications)
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// ApplicationsPost response for add user's access token
|
2021-01-26 21:06:53 +05:30
|
|
|
func ApplicationsPost(ctx *context.Context) {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.NewAccessTokenForm)
|
2018-05-17 09:35:00 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsApplications"] = true
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2018-06-18 23:54:45 +05:30
|
|
|
loadApplicationsData(ctx)
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplSettingsApplications)
|
2018-05-17 09:35:00 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-18 03:16:03 +05:30
|
|
|
scope, err := form.GetScope()
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetScope", err)
|
|
|
|
return
|
|
|
|
}
|
2022-08-25 08:01:57 +05:30
|
|
|
t := &auth_model.AccessToken{
|
2023-01-18 03:16:03 +05:30
|
|
|
UID: ctx.Doer.ID,
|
|
|
|
Name: form.Name,
|
|
|
|
Scope: scope,
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
2020-04-14 00:32:48 +05:30
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
exist, err := auth_model.AccessTokenByNameExists(t)
|
2020-04-14 00:32:48 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("AccessTokenByNameExists", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if exist {
|
|
|
|
ctx.Flash.Error(ctx.Tr("settings.generate_token_name_duplicate", t.Name))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/applications")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
if err := auth_model.NewAccessToken(t); err != nil {
|
2018-05-17 09:35:00 +05:30
|
|
|
ctx.ServerError("NewAccessToken", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.generate_token_success"))
|
2019-05-04 21:15:34 +05:30
|
|
|
ctx.Flash.Info(t.Token)
|
2018-05-17 09:35:00 +05:30
|
|
|
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/applications")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteApplication response for delete user access token
|
|
|
|
func DeleteApplication(ctx *context.Context) {
|
2022-08-25 08:01:57 +05:30
|
|
|
if err := auth_model.DeleteAccessTokenByID(ctx.FormInt64("id"), ctx.Doer.ID); err != nil {
|
2018-05-17 09:35:00 +05:30
|
|
|
ctx.Flash.Error("DeleteAccessTokenByID: " + err.Error())
|
|
|
|
} else {
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.delete_token_success"))
|
|
|
|
}
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2018-05-17 09:35:00 +05:30
|
|
|
"redirect": setting.AppSubURL + "/user/settings/applications",
|
|
|
|
})
|
|
|
|
}
|
2018-06-18 23:54:45 +05:30
|
|
|
|
|
|
|
func loadApplicationsData(ctx *context.Context) {
|
2022-08-25 08:01:57 +05:30
|
|
|
tokens, err := auth_model.ListAccessTokens(auth_model.ListAccessTokensOptions{UserID: ctx.Doer.ID})
|
2018-06-18 23:54:45 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("ListAccessTokens", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Tokens"] = tokens
|
2019-03-08 22:12:50 +05:30
|
|
|
ctx.Data["EnableOAuth2"] = setting.OAuth2.Enable
|
|
|
|
if setting.OAuth2.Enable {
|
2022-08-25 08:01:57 +05:30
|
|
|
ctx.Data["Applications"], err = auth_model.GetOAuth2ApplicationsByUserID(ctx, ctx.Doer.ID)
|
2019-03-08 22:12:50 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetOAuth2ApplicationsByUserID", err)
|
|
|
|
return
|
|
|
|
}
|
2022-08-25 08:01:57 +05:30
|
|
|
ctx.Data["Grants"], err = auth_model.GetOAuth2GrantsByUserID(ctx, ctx.Doer.ID)
|
2019-04-17 13:48:16 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetOAuth2GrantsByUserID", err)
|
|
|
|
return
|
|
|
|
}
|
2019-03-08 22:12:50 +05:30
|
|
|
}
|
2018-06-18 23:54:45 +05:30
|
|
|
}
|