This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
forked from SUSE/stratos
/
http_basic_requests.go
53 lines (41 loc) · 1.99 KB
/
http_basic_requests.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
package main
import (
"encoding/base64"
"errors"
"net/http"
log "github.com/sirupsen/logrus"
"github.com/epinio/ui-backend/src/jetstream/repository/interfaces"
)
func (p *portalProxy) doHttpBasicFlowRequest(cnsiRequest *interfaces.CNSIRequest, req *http.Request) (*http.Response, error) {
log.Debug("doHttpBasicFlowRequest")
authHandler := func(tokenRec interfaces.TokenRecord, cnsi interfaces.CNSIRecord) (*http.Response, error) {
// Http Basic has no token refresh or expiry - so much simpler than the OAuth flow
req.Header.Set("Authorization", "Basic "+tokenRec.AuthToken)
client := p.GetHttpClientForRequest(req, cnsi.SkipSSLValidation)
return client.Do(req)
}
return p.DoAuthFlowRequest(cnsiRequest, req, authHandler)
}
func (p *portalProxy) doBearerFlowRequest(cnsiRequest *interfaces.CNSIRequest, req *http.Request) (*http.Response, error) {
log.Debug("doBearerFlowRequest")
return p.doAuthHeaderFlowRequest("bearer", cnsiRequest, req)
}
func (p *portalProxy) doTokenFlowRequest(cnsiRequest *interfaces.CNSIRequest, req *http.Request) (*http.Response, error) {
log.Debug("doTokenFlowRequest")
return p.doAuthHeaderFlowRequest("token", cnsiRequest, req)
}
// Auth where a toekn is passed in the HTTP Authorization
func (p *portalProxy) doAuthHeaderFlowRequest(headerPrefix string, cnsiRequest *interfaces.CNSIRequest, req *http.Request) (*http.Response, error) {
log.Debug("doAuthHeaderFlowRequest")
authHandler := func(tokenRec interfaces.TokenRecord, cnsi interfaces.CNSIRecord) (*http.Response, error) {
authTokenDecodedBytes, err := base64.StdEncoding.DecodeString(tokenRec.AuthToken)
if err != nil {
return nil, errors.New("Failed to decode auth token")
}
// Token auth has no token refresh or expiry - so much simpler than the OAuth flow
req.Header.Set("Authorization", headerPrefix+" "+string(authTokenDecodedBytes))
client := p.GetHttpClientForRequest(req, cnsi.SkipSSLValidation)
return client.Do(req)
}
return p.DoAuthFlowRequest(cnsiRequest, req, authHandler)
}