From da0f6fb366981cedf214e638db105e5ff7be5b85 Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 2 Apr 2025 17:47:13 +0200 Subject: [PATCH] feat(auth): allow passing custom settings links to user account via openid claims --- pkg/migration/20250402173109.go | 43 +++++++++++++++++++++++++++++++ pkg/modules/auth/openid/openid.go | 30 ++++++++++++--------- pkg/user/user.go | 4 ++- 3 files changed, 64 insertions(+), 13 deletions(-) create mode 100644 pkg/migration/20250402173109.go diff --git a/pkg/migration/20250402173109.go b/pkg/migration/20250402173109.go new file mode 100644 index 000000000..e514f5a98 --- /dev/null +++ b/pkg/migration/20250402173109.go @@ -0,0 +1,43 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-present Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package migration + +import ( + "src.techknowlogick.com/xormigrate" + "xorm.io/xorm" +) + +type users20250402173109 struct { + ExtraSettingsLinks map[string]any `xorm:"json null" json:"-"` +} + +func (users20250402173109) TableName() string { + return "users" +} + +func init() { + migrations = append(migrations, &xormigrate.Migration{ + ID: "20250402173109", + Description: "add extra settings links", + Migrate: func(tx *xorm.Engine) error { + return tx.Sync(users20250402173109{}) + }, + Rollback: func(tx *xorm.Engine) error { + return nil + }, + }) +} diff --git a/pkg/modules/auth/openid/openid.go b/pkg/modules/auth/openid/openid.go index b2eb982b5..5b26835f7 100644 --- a/pkg/modules/auth/openid/openid.go +++ b/pkg/modules/auth/openid/openid.go @@ -68,12 +68,13 @@ type Provider struct { } type claims struct { - Email string `json:"email"` - Name string `json:"name"` - PreferredUsername string `json:"preferred_username"` - Nickname string `json:"nickname"` - VikunjaGroups []map[string]interface{} `json:"vikunja_groups"` - Picture string `json:"picture"` + Email string `json:"email"` + Name string `json:"name"` + PreferredUsername string `json:"preferred_username"` + Nickname string `json:"nickname"` + VikunjaGroups []map[string]interface{} `json:"vikunja_groups"` + Picture string `json:"picture"` + ExtraSettingsLinks map[string]any `json:"extra_settings_links"` } func init() { @@ -304,13 +305,15 @@ func getOrCreateUser(s *xorm.Session, cl *claims, provider *Provider, idToken *o // If no user exists, create one with the preferred username if it is not already taken uu := &user.User{ - Username: strings.ReplaceAll(cl.PreferredUsername, " ", "-"), - Email: cl.Email, - Name: cl.Name, - Status: user.StatusActive, - Issuer: idToken.Issuer, - Subject: idToken.Subject, + Username: strings.ReplaceAll(cl.PreferredUsername, " ", "-"), + Email: cl.Email, + Name: cl.Name, + Status: user.StatusActive, + Issuer: idToken.Issuer, + Subject: idToken.Subject, + ExtraSettingsLinks: cl.ExtraSettingsLinks, } + u, err = auth.CreateUserWithRandomUsername(s, uu) if err != nil { return nil, err @@ -324,6 +327,9 @@ func getOrCreateUser(s *xorm.Session, cl *claims, provider *Provider, idToken *o if cl.Name != u.Name { u.Name = cl.Name } + + u.ExtraSettingsLinks = cl.ExtraSettingsLinks + u, err = user.UpdateUser(s, u, false) if err != nil { return nil, err diff --git a/pkg/user/user.go b/pkg/user/user.go index 1479a8d3e..6f6d44662 100644 --- a/pkg/user/user.go +++ b/pkg/user/user.go @@ -105,7 +105,8 @@ type User struct { DeletionScheduledAt time.Time `xorm:"datetime null" json:"-"` DeletionLastReminderSent time.Time `xorm:"datetime null" json:"-"` - FrontendSettings interface{} `xorm:"json null" json:"-"` + FrontendSettings interface{} `xorm:"json null" json:"-"` + ExtraSettingsLinks map[string]any `xorm:"json null" json:"-"` ExportFileID int64 `xorm:"bigint null" json:"-"` @@ -607,6 +608,7 @@ func UpdateUser(s *xorm.Session, user *User, forceOverride bool) (updatedUser *U "timezone", "overdue_tasks_reminders_time", "frontend_settings", + "extra_settings_links", ). Update(user) if err != nil {