code.gitea.io/gitea@v1.22.3/routers/web/org/setting_oauth2.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package org
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  
    10  	"code.gitea.io/gitea/models/auth"
    11  	"code.gitea.io/gitea/models/db"
    12  	"code.gitea.io/gitea/modules/base"
    13  	"code.gitea.io/gitea/modules/setting"
    14  	shared_user "code.gitea.io/gitea/routers/web/shared/user"
    15  	user_setting "code.gitea.io/gitea/routers/web/user/setting"
    16  	"code.gitea.io/gitea/services/context"
    17  )
    18  
    19  const (
    20  	tplSettingsApplications         base.TplName = "org/settings/applications"
    21  	tplSettingsOAuthApplicationEdit base.TplName = "org/settings/applications_oauth2_edit"
    22  )
    23  
    24  func newOAuth2CommonHandlers(org *context.Organization) *user_setting.OAuth2CommonHandlers {
    25  	return &user_setting.OAuth2CommonHandlers{
    26  		OwnerID:            org.Organization.ID,
    27  		BasePathList:       fmt.Sprintf("%s/org/%s/settings/applications", setting.AppSubURL, org.Organization.Name),
    28  		BasePathEditPrefix: fmt.Sprintf("%s/org/%s/settings/applications/oauth2", setting.AppSubURL, org.Organization.Name),
    29  		TplAppEdit:         tplSettingsOAuthApplicationEdit,
    30  	}
    31  }
    32  
    33  // Applications render org applications page (for org, at the moment, there are only OAuth2 applications)
    34  func Applications(ctx *context.Context) {
    35  	ctx.Data["Title"] = ctx.Tr("settings.applications")
    36  	ctx.Data["PageIsOrgSettings"] = true
    37  	ctx.Data["PageIsSettingsApplications"] = true
    38  
    39  	apps, err := db.Find[auth.OAuth2Application](ctx, auth.FindOAuth2ApplicationsOptions{
    40  		OwnerID: ctx.Org.Organization.ID,
    41  	})
    42  	if err != nil {
    43  		ctx.ServerError("GetOAuth2ApplicationsByUserID", err)
    44  		return
    45  	}
    46  	ctx.Data["Applications"] = apps
    47  
    48  	err = shared_user.LoadHeaderCount(ctx)
    49  	if err != nil {
    50  		ctx.ServerError("LoadHeaderCount", err)
    51  		return
    52  	}
    53  
    54  	ctx.HTML(http.StatusOK, tplSettingsApplications)
    55  }
    56  
    57  // OAuthApplicationsPost response for adding an oauth2 application
    58  func OAuthApplicationsPost(ctx *context.Context) {
    59  	ctx.Data["Title"] = ctx.Tr("settings.applications")
    60  	ctx.Data["PageIsOrgSettings"] = true
    61  	ctx.Data["PageIsSettingsApplications"] = true
    62  
    63  	oa := newOAuth2CommonHandlers(ctx.Org)
    64  	oa.AddApp(ctx)
    65  }
    66  
    67  // OAuth2ApplicationShow displays the given application
    68  func OAuth2ApplicationShow(ctx *context.Context) {
    69  	ctx.Data["PageIsOrgSettings"] = true
    70  	ctx.Data["PageIsSettingsApplications"] = true
    71  
    72  	oa := newOAuth2CommonHandlers(ctx.Org)
    73  	oa.EditShow(ctx)
    74  }
    75  
    76  // OAuth2ApplicationEdit response for editing oauth2 application
    77  func OAuth2ApplicationEdit(ctx *context.Context) {
    78  	ctx.Data["Title"] = ctx.Tr("settings.applications")
    79  	ctx.Data["PageIsOrgSettings"] = true
    80  	ctx.Data["PageIsSettingsApplications"] = true
    81  
    82  	oa := newOAuth2CommonHandlers(ctx.Org)
    83  	oa.EditSave(ctx)
    84  }
    85  
    86  // OAuthApplicationsRegenerateSecret handles the post request for regenerating the secret
    87  func OAuthApplicationsRegenerateSecret(ctx *context.Context) {
    88  	ctx.Data["Title"] = ctx.Tr("settings")
    89  	ctx.Data["PageIsOrgSettings"] = true
    90  	ctx.Data["PageIsSettingsApplications"] = true
    91  
    92  	oa := newOAuth2CommonHandlers(ctx.Org)
    93  	oa.RegenerateSecret(ctx)
    94  }
    95  
    96  // DeleteOAuth2Application deletes the given oauth2 application
    97  func DeleteOAuth2Application(ctx *context.Context) {
    98  	oa := newOAuth2CommonHandlers(ctx.Org)
    99  	oa.DeleteApp(ctx)
   100  }
   101  
   102  // TODO: revokes the grant with the given id