FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sourcegraph/internal/database/authenticator.go at main · oxenprogrammer/sourcegraph · GitHub
oxenprogrammer
/
sourcegraph
Public
forked from
sourcegraph/sourcegraph-public-snapshot
Notifications
You must be signed in to change notification settings
Fork
1
Star
1
Code
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sourcegraph
/
internal
/
database
/
authenticator.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
150 lines (131 loc) · 4.67 KB
Breadcrumbs
sourcegraph
/
internal
/
database
/
authenticator.go
Copy path
File metadata and controls
150 lines (131 loc) · 4.67 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package
database
import
(
"context"
"database/sql/driver"
"encoding/json"
"github.com/cockroachdb/errors"
"github.com/sourcegraph/sourcegraph/internal/encryption"
"github.com/sourcegraph/sourcegraph/internal/extsvc/auth"
"github.com/sourcegraph/sourcegraph/internal/extsvc/bitbucketserver"
"github.com/sourcegraph/sourcegraph/internal/extsvc/gitlab"
)
// AuthenticatorType defines all possible types of authenticators stored in the database.
type
AuthenticatorType
string
// Define credential type strings that we'll use when encoding credentials.
const
(
AuthenticatorTypeOAuthClient
AuthenticatorType
=
"OAuthClient"
AuthenticatorTypeBasicAuth
AuthenticatorType
=
"BasicAuth"
AuthenticatorTypeBasicAuthWithSSH
AuthenticatorType
=
"BasicAuthWithSSH"
AuthenticatorTypeOAuthBearerToken
AuthenticatorType
=
"OAuthBearerToken"
AuthenticatorTypeOAuthBearerTokenWithSSH
AuthenticatorType
=
"OAuthBearerTokenWithSSH"
AuthenticatorTypeBitbucketServerSudoableOAuthClient
AuthenticatorType
=
"BitbucketSudoableOAuthClient"
AuthenticatorTypeGitLabSudoableToken
AuthenticatorType
=
"GitLabSudoableToken"
)
// NullAuthenticator represents an authenticator that may be null. It implements
// the sql.Scanner interface so it can be used as a scan destination, similar to
// sql.NullString. When the scanned value is null, the authenticator will be nil.
// It handles marshalling and unmarshalling the authenticator from and to JSON.
type
NullAuthenticator
struct
{
A
*
auth.
Authenticator
}
// Scan implements the Scanner interface.
func
(
n
*
NullAuthenticator
)
Scan
(
value
interface
{}) (
err
error
) {
switch
value
:=
value
.(
type
) {
case
string
:
*
n
.
A
,
err
=
UnmarshalAuthenticator
(
value
)
return
err
case
nil
:
return
nil
default
:
return
errors
.
Errorf
(
"value is not string: %T"
,
value
)
}
}
// Value implements the driver Valuer interface.
func
(
n
NullAuthenticator
)
Value
() (driver.
Value
,
error
) {
if
*
n
.
A
==
nil
{
return
nil
,
nil
}
return
marshalAuthenticator
(
*
n
.
A
)
}
// EncryptAuthenticator encodes _and_ encrypts an Authenticator into a byte
// slice.
func
EncryptAuthenticator
(
ctx
context.
Context
,
enc
encryption.
Encrypter
,
a
auth.
Authenticator
) ([]
byte
,
error
) {
raw
,
err
:=
marshalAuthenticator
(
a
)
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"marshalling authenticator"
)
}
if
enc
==
nil
{
return
[]
byte
(
raw
),
nil
}
secret
,
err
:=
enc
.
Encrypt
(
ctx
, []
byte
(
raw
))
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"encrypting credential"
)
}
return
secret
,
nil
}
// marshalAuthenticator encodes an Authenticator into a JSON string.
func
marshalAuthenticator
(
a
auth.
Authenticator
) (
string
,
error
) {
var
t
AuthenticatorType
switch
a
.(
type
) {
case
*
auth.
OAuthClient
:
t
=
AuthenticatorTypeOAuthClient
case
*
auth.
BasicAuth
:
t
=
AuthenticatorTypeBasicAuth
case
*
auth.
BasicAuthWithSSH
:
t
=
AuthenticatorTypeBasicAuthWithSSH
case
*
auth.
OAuthBearerToken
:
t
=
AuthenticatorTypeOAuthBearerToken
case
*
auth.
OAuthBearerTokenWithSSH
:
t
=
AuthenticatorTypeOAuthBearerTokenWithSSH
case
*
bitbucketserver.
SudoableOAuthClient
:
t
=
AuthenticatorTypeBitbucketServerSudoableOAuthClient
case
*
gitlab.
SudoableToken
:
t
=
AuthenticatorTypeGitLabSudoableToken
default
:
return
""
,
errors
.
Errorf
(
"unknown Authenticator implementation type: %T"
,
a
)
}
raw
,
err
:=
json
.
Marshal
(
struct
{
Type
AuthenticatorType
Auth
auth.
Authenticator
}{
Type
:
t
,
Auth
:
a
,
})
if
err
!=
nil
{
return
""
,
err
}
return
string
(
raw
),
nil
}
// UnmarshalAuthenticator decodes a JSON string into an Authenticator.
func
UnmarshalAuthenticator
(
raw
string
) (auth.
Authenticator
,
error
) {
// We do two unmarshals: the first just to get the type, and then the second
// to actually unmarshal the authenticator itself.
var
partial
struct
{
Type
AuthenticatorType
Auth
json.
RawMessage
}
if
err
:=
json
.
Unmarshal
([]
byte
(
raw
),
&
partial
);
err
!=
nil
{
return
nil
,
err
}
var
a
interface
{}
switch
partial
.
Type
{
case
AuthenticatorTypeOAuthClient
:
a
=
&
auth.
OAuthClient
{}
case
AuthenticatorTypeBasicAuth
:
a
=
&
auth.
BasicAuth
{}
case
AuthenticatorTypeBasicAuthWithSSH
:
a
=
&
auth.
BasicAuthWithSSH
{}
case
AuthenticatorTypeOAuthBearerToken
:
a
=
&
auth.
OAuthBearerToken
{}
case
AuthenticatorTypeOAuthBearerTokenWithSSH
:
a
=
&
auth.
OAuthBearerTokenWithSSH
{}
case
AuthenticatorTypeBitbucketServerSudoableOAuthClient
:
a
=
&
bitbucketserver.
SudoableOAuthClient
{}
case
AuthenticatorTypeGitLabSudoableToken
:
a
=
&
gitlab.
SudoableToken
{}
default
:
return
nil
,
errors
.
Errorf
(
"unknown credential type: %s"
,
partial
.
Type
)
}
if
err
:=
json
.
Unmarshal
(
partial
.
Auth
,
&
a
);
err
!=
nil
{
return
nil
,
err
}
return
a
.(auth.
Authenticator
),
nil
}
Back
|
FazBrowse Home
|
New Git URL