This repository was archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathauth.go
58 lines (51 loc) · 1.67 KB
/
auth.go
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
package types
import (
"encoding/base64"
"encoding/json"
"errors"
"net/http"
"maunium.net/go/mautrix/id"
)
// AuthRealm represents a place where a user can authenticate themselves.
// This may static (like github.com) or a specific domain (like matrix.org/jira)
type AuthRealm interface {
ID() string
Type() string
Init() error
Register() error
OnReceiveRedirect(w http.ResponseWriter, req *http.Request)
AuthSession(id string, userID id.UserID, realmID string) AuthSession
RequestAuthSession(userID id.UserID, config json.RawMessage) interface{}
}
var realmsByType = map[string]func(string, string) AuthRealm{}
// RegisterAuthRealm registers a factory for creating AuthRealm instances.
func RegisterAuthRealm(factory func(string, string) AuthRealm) {
realmsByType[factory("", "").Type()] = factory
}
// CreateAuthRealm creates an AuthRealm of the given type and realm ID.
// Returns an error if the realm couldn't be created or the JSON cannot be unmarshalled.
func CreateAuthRealm(realmID, realmType string, realmJSON []byte) (AuthRealm, error) {
f := realmsByType[realmType]
if f == nil {
return nil, errors.New("Unknown realm type: " + realmType)
}
base64RealmID := base64.RawURLEncoding.EncodeToString([]byte(realmID))
redirectURL := baseURL + "realms/redirects/" + base64RealmID
r := f(realmID, redirectURL)
if err := json.Unmarshal(realmJSON, r); err != nil {
return nil, err
}
if err := r.Init(); err != nil {
return nil, err
}
return r, nil
}
// AuthSession represents a single authentication session between a user and
// an auth realm.
type AuthSession interface {
ID() string
UserID() id.UserID
RealmID() string
Authenticated() bool
Info() interface{}
}