File: authheader.go

package info (click to toggle)
golang-github-azure-go-ntlmssp 0.0~git20220621.cb9428e-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, experimental, forky, sid, trixie
  • size: 148 kB
  • sloc: makefile: 2
file content (66 lines) | stat: -rw-r--r-- 1,249 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ntlmssp

import (
	"encoding/base64"
	"strings"
)

type authheader []string

func (h authheader) IsBasic() bool {
	for _, s := range h {
		if strings.HasPrefix(string(s), "Basic ") {
			return true
		}
	}
	return false
}

func (h authheader) Basic() string {
	for _, s := range h {
		if strings.HasPrefix(string(s), "Basic ") {
			return s
		}
	}
	return ""
}

func (h authheader) IsNegotiate() bool {
	for _, s := range h {
		if strings.HasPrefix(string(s), "Negotiate") {
			return true
		}
	}
	return false
}

func (h authheader) IsNTLM() bool {
	for _, s := range h {
		if strings.HasPrefix(string(s), "NTLM") {
			return true
		}
	}
	return false
}

func (h authheader) GetData() ([]byte, error) {
	for _, s := range h {
		if strings.HasPrefix(string(s), "NTLM") || strings.HasPrefix(string(s), "Negotiate") || strings.HasPrefix(string(s), "Basic ") {
			p := strings.Split(string(s), " ")
			if len(p) < 2 {
				return nil, nil
			}
			return base64.StdEncoding.DecodeString(string(p[1]))
		}
	}
	return nil, nil
}

func (h authheader) GetBasicCreds() (username, password string, err error) {
	d, err := h.GetData()
	if err != nil {
		return "", "", err
	}
	parts := strings.SplitN(string(d), ":", 2)
	return parts[0], parts[1], nil
}