forked from vmware-archive/atc
/
get_auth_token.go
55 lines (47 loc) · 1.37 KB
/
get_auth_token.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
package authserver
import (
"encoding/json"
"net/http"
"strings"
"time"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/atc/auth"
)
func (s *Server) GetAuthToken(w http.ResponseWriter, r *http.Request) {
logger := s.logger.Session("get-auth-token")
logger.Debug("getting-auth-token")
authorization := r.Header.Get("Authorization")
authSegs := strings.SplitN(authorization, " ", 2)
var token atc.AuthToken
if strings.ToLower(authSegs[0]) == strings.ToLower(auth.TokenTypeBearer) {
token.Type = authSegs[0]
token.Value = authSegs[1]
} else {
teamName := r.FormValue(":team_name")
teamDB := s.teamDBFactory.GetTeamDB(teamName)
team, found, err := teamDB.GetTeam()
if err != nil {
logger.Error("get-team-by-name", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
logger.Info("cannot-find-team-by-name", lager.Data{
"teamName": teamName,
})
w.WriteHeader(http.StatusUnauthorized)
return
}
tokenType, tokenValue, err := s.tokenGenerator.GenerateToken(time.Now().Add(s.expire), team.Name, team.ID, team.Admin)
if err != nil {
logger.Error("generate-token", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
token.Type = string(tokenType)
token.Value = string(tokenValue)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(token)
}