File: validate.go

package info (click to toggle)
tea-cli 0.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,648 kB
  • sloc: makefile: 116; sh: 17
file content (38 lines) | stat: -rw-r--r-- 962 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package utils

import (
	"fmt"
	"net/url"
)

// ValidateAuthenticationMethod checks the provided authentication method parameters
func ValidateAuthenticationMethod(
	giteaURL string,
	token string,
	user string,
	passwd string,
	sshAgent bool,
	sshKey string,
	sshCertPrincipal string,
) (*url.URL, error) {
	// Normalize URL
	serverURL, err := NormalizeURL(giteaURL)
	if err != nil {
		return nil, fmt.Errorf("Unable to parse URL: %s", err)
	}

	if !sshAgent && sshCertPrincipal == "" && sshKey == "" {
		// .. if we have enough information to authenticate
		if len(token) == 0 && (len(user)+len(passwd)) == 0 {
			return nil, fmt.Errorf("No token set")
		} else if len(user) != 0 && len(passwd) == 0 {
			return nil, fmt.Errorf("No password set")
		} else if len(user) == 0 && len(passwd) != 0 {
			return nil, fmt.Errorf("No user set")
		}
	}
	return serverURL, nil
}