Skip to content

Commit e763875

Browse files
committed
wip: can show login info
1 parent 9032055 commit e763875

File tree

9 files changed

+3464
-8
lines changed

9 files changed

+3464
-8
lines changed

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,17 @@ check-format: ## Check that sources are correctly formatted
6868
.PHONY: check-vet
6969
check-vet: ## Check source files with `go vet`
7070
go vet ./...
71+
72+
##@ Code generation
73+
74+
.PHONY: renku-users-apispec
75+
renku-users-apispec: ## Download the "users" API spec
76+
curl -L -o pkg/renkuapi/users/api.spec.yaml https://raw.githubusercontent.com/SwissDataScienceCenter/renku-data-services/refs/heads/main/components/renku_data_services/users/api.spec.yaml
77+
sed -e 's/- default: "general"//g' pkg/renkuapi/users/api.spec.yaml > pkg/renkuapi/users/api.spec.new.yaml
78+
mv pkg/renkuapi/users/api.spec.new.yaml pkg/renkuapi/users/api.spec.yaml
79+
80+
.PHONY: generate
81+
generate: pkg/renkuapi/users/users_gen.go ## Run go generate
82+
83+
pkg/renkuapi/users/users_gen.go: pkg/renkuapi/users/api.spec.yaml
84+
go generate pkg/renkuapi/users/users.go

go.mod

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ module github.com/SwissDataScienceCenter/renku-dev-utils
33
go 1.24.2
44

55
require (
6+
github.com/getkin/kin-openapi v0.132.0
67
github.com/golang-jwt/jwt/v5 v5.3.0
8+
github.com/oapi-codegen/oapi-codegen/v2 v2.5.0
9+
github.com/oapi-codegen/runtime v1.1.2
710
github.com/spf13/cobra v1.10.1
811
github.com/zalando/go-keyring v0.2.6
912
k8s.io/api v0.34.0
@@ -12,8 +15,10 @@ require (
1215

1316
require (
1417
al.essio.dev/pkg/shellescape v1.5.1 // indirect
18+
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
1519
github.com/danieljoos/wincred v1.2.2 // indirect
1620
github.com/davecgh/go-spew v1.1.1 // indirect
21+
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
1722
github.com/emicklei/go-restful/v3 v3.12.2 // indirect
1823
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
1924
github.com/go-logr/logr v1.4.2 // indirect
@@ -29,23 +34,34 @@ require (
2934
github.com/mailru/easyjson v0.7.7 // indirect
3035
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
3136
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
37+
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
3238
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
39+
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 // indirect
40+
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect
41+
github.com/perimeterx/marshmallow v1.1.5 // indirect
3342
github.com/pkg/errors v0.9.1 // indirect
43+
github.com/speakeasy-api/jsonpath v0.6.0 // indirect
44+
github.com/speakeasy-api/openapi-overlay v0.10.2 // indirect
45+
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
3446
github.com/x448/float16 v0.8.4 // indirect
3547
go.yaml.in/yaml/v2 v2.4.2 // indirect
3648
go.yaml.in/yaml/v3 v3.0.4 // indirect
3749
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
3850
golang.org/x/image v0.28.0 // indirect
3951
golang.org/x/mobile v0.0.0-20250606033058-a2a15c67f36f // indirect
40-
golang.org/x/net v0.38.0 // indirect
52+
golang.org/x/mod v0.25.0 // indirect
53+
golang.org/x/net v0.41.0 // indirect
4154
golang.org/x/oauth2 v0.27.0 // indirect
55+
golang.org/x/sync v0.15.0 // indirect
4256
golang.org/x/sys v0.33.0 // indirect
43-
golang.org/x/term v0.30.0 // indirect
57+
golang.org/x/term v0.32.0 // indirect
4458
golang.org/x/text v0.26.0 // indirect
4559
golang.org/x/time v0.9.0 // indirect
60+
golang.org/x/tools v0.34.0 // indirect
4661
google.golang.org/protobuf v1.36.5 // indirect
4762
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
4863
gopkg.in/inf.v0 v0.9.1 // indirect
64+
gopkg.in/yaml.v2 v2.4.0 // indirect
4965
gopkg.in/yaml.v3 v3.0.1 // indirect
5066
k8s.io/klog/v2 v2.130.1 // indirect
5167
k8s.io/kube-openapi v0.0.0-20250710124328-f3f2b991d03b // indirect

go.sum

Lines changed: 124 additions & 4 deletions
Large diffs are not rendered by default.

pkg/cmd/login.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/SwissDataScienceCenter/renku-dev-utils/pkg/github"
99
ns "github.com/SwissDataScienceCenter/renku-dev-utils/pkg/namespace"
1010
"github.com/SwissDataScienceCenter/renku-dev-utils/pkg/renkuapi"
11+
"github.com/SwissDataScienceCenter/renku-dev-utils/pkg/renkuapi/users"
1112
"github.com/spf13/cobra"
1213
)
1314

@@ -53,7 +54,7 @@ func login(cmd *cobra.Command, args []string) {
5354
url = deploymentURL.String()
5455
}
5556

56-
fmt.Printf("URL '%s'\n", url)
57+
fmt.Printf("Renku URL: %s\n", url)
5758

5859
auth, err := renkuapi.NewRenkuApiAuth(url)
5960
if err != nil {
@@ -66,6 +67,26 @@ func login(cmd *cobra.Command, args []string) {
6667
fmt.Println(err)
6768
os.Exit(1)
6869
}
70+
71+
ruc, err := users.NewRenkuUsersClient(url, users.WithRequestEditors(users.RequestEditorFn(auth.RequestEditor())))
72+
if err != nil {
73+
fmt.Println(err)
74+
os.Exit(1)
75+
}
76+
77+
userInfo, err := ruc.GetUser(ctx)
78+
if err != nil {
79+
fmt.Println(err)
80+
os.Exit(1)
81+
}
82+
83+
fmt.Println("Logged in as:")
84+
fmt.Printf(" username: %s\n", userInfo.Username)
85+
fmt.Printf(" email: %s\n", *userInfo.Email)
86+
fmt.Printf(" first name: %s\n", *userInfo.FirstName)
87+
fmt.Printf(" last name: %s\n", *userInfo.LastName)
88+
fmt.Printf(" is admin: %t\n", userInfo.IsAdmin)
89+
6990
}
7091

7192
func init() {

pkg/renkuapi/auth.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ func NewRenkuApiAuth(baseURL string) (auth *RenkuApiAuth, err error) {
6060
return auth, nil
6161
}
6262

63+
// RequestEditorFn is the function signature for the RequestEditor callback function
64+
type RequestEditorFn func(ctx context.Context, req *http.Request) error
65+
66+
// RequestEditor returns a request editor which injects a valid access token
67+
// for API requests.
68+
func (auth *RenkuApiAuth) RequestEditor() RequestEditorFn {
69+
return func(ctx context.Context, req *http.Request) error {
70+
if req.Header.Get("Authorization") != "" {
71+
return nil
72+
}
73+
token, err := auth.GetAccessToken(ctx)
74+
if err != nil {
75+
return err
76+
}
77+
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
78+
return nil
79+
}
80+
}
81+
6382
func (auth *RenkuApiAuth) GetAccessToken(ctx context.Context) (token string, err error) {
6483
// Use access token if valid
6584
token = auth.accessToken
@@ -154,7 +173,6 @@ func (auth *RenkuApiAuth) getKeyringUserPrefix() string {
154173
}
155174

156175
func (auth *RenkuApiAuth) Login(ctx context.Context) error {
157-
// TODO: check username from API
158176
token, _ := auth.GetAccessToken(ctx)
159177
if token != "" {
160178
return nil

0 commit comments

Comments
 (0)