harbor/service/token/token.go

99 lines
2.9 KiB
Go
Raw Normal View History

2016-02-01 12:59:10 +01:00
/*
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2016-02-26 11:54:14 +01:00
2016-04-15 07:17:32 +02:00
package token
2016-02-01 12:59:10 +01:00
import (
"net/http"
2016-04-29 08:59:00 +02:00
"time"
2016-02-01 12:59:10 +01:00
"github.com/vmware/harbor/auth"
2016-02-01 12:59:10 +01:00
"github.com/vmware/harbor/models"
svc_utils "github.com/vmware/harbor/service/utils"
2016-03-25 02:31:50 +01:00
"github.com/vmware/harbor/utils/log"
2016-02-01 12:59:10 +01:00
"github.com/astaxie/beego"
"github.com/docker/distribution/registry/auth/token"
)
2016-04-15 07:17:32 +02:00
// Handler handles request on /service/token, which is the auth provider for registry.
type Handler struct {
2016-02-01 12:59:10 +01:00
beego.Controller
}
2016-02-26 11:35:55 +01:00
// Get handles GET request, it checks the http header for user credentials
// and parse service and scope based on docker registry v2 standard,
// checkes the permission agains local DB and generates jwt token.
2016-04-15 07:17:32 +02:00
func (h *Handler) Get() {
2016-02-01 12:59:10 +01:00
var uid, password, username string
2016-04-15 07:17:32 +02:00
request := h.Ctx.Request
service := h.GetString("service")
scopes := h.GetStrings("scope")
access := GetResourceActions(scopes)
log.Infof("request url: %v", request.URL.String())
if svc_utils.VerifySecret(request) {
log.Debugf("Will grant all access as this request is from job service with legal secret.")
username = "job-service-user"
} else {
uid, password, _ = request.BasicAuth()
log.Debugf("uid for logging: %s", uid)
user := authenticate(uid, password)
if user == nil {
log.Warningf("login request with invalid credentials in token service, uid: %s", uid)
if len(scopes) == 0 {
h.CustomAbort(http.StatusUnauthorized, "")
}
} else {
username = user.Username
}
log.Debugf("username for filtering access: %s.", username)
for _, a := range access {
FilterAccess(username, a)
}
2016-02-01 12:59:10 +01:00
}
2016-04-15 07:17:32 +02:00
h.serveToken(username, service, access)
2016-02-01 12:59:10 +01:00
}
2016-04-15 07:17:32 +02:00
func (h *Handler) serveToken(username, service string, access []*token.ResourceActions) {
writer := h.Ctx.ResponseWriter
2016-02-01 12:59:10 +01:00
//create token
2016-04-29 08:59:00 +02:00
rawToken, expiresIn, issuedAt, err := MakeToken(username, service, access)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-03-25 02:08:44 +01:00
log.Errorf("Failed to make token, error: %v", err)
writer.WriteHeader(http.StatusInternalServerError)
2016-02-01 12:59:10 +01:00
return
}
2016-04-29 08:59:00 +02:00
tk := make(map[string]interface{})
2016-02-01 12:59:10 +01:00
tk["token"] = rawToken
2016-04-29 08:59:00 +02:00
tk["expires_in"] = expiresIn
tk["issued_at"] = issuedAt.Format(time.RFC3339)
2016-04-15 07:17:32 +02:00
h.Data["json"] = tk
h.ServeJSON()
2016-02-01 12:59:10 +01:00
}
func authenticate(principal, password string) *models.User {
2016-04-15 07:17:32 +02:00
user, err := auth.Login(models.AuthModel{
Principal: principal,
Password: password,
})
2016-02-01 12:59:10 +01:00
if err != nil {
2016-03-25 02:08:44 +01:00
log.Errorf("Error occurred in UserLogin: %v", err)
return nil
2016-02-01 12:59:10 +01:00
}
return user
2016-02-01 12:59:10 +01:00
}