minio/web-handlers.go

152 lines
3.9 KiB
Go
Raw Normal View History

/*
* Minio Cloud Storage, (C) 2016 Minio, Inc.
*
* 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.
*/
package main
import (
"fmt"
"net/http"
"time"
2016-01-25 01:26:53 -05:00
jwtgo "github.com/dgrijalva/jwt-go"
2016-01-26 15:08:45 -05:00
"github.com/minio/minio/pkg/disk"
)
// isAuthenticated validates if any incoming request to be a valid JWT
// authenticated request.
func isAuthenticated(req *http.Request) bool {
2016-01-25 01:26:53 -05:00
jwt := InitJWT()
tokenRequest, err := jwtgo.ParseFromRequest(req, func(token *jwtgo.Token) (interface{}, error) {
if _, ok := token.Method.(*jwtgo.SigningMethodRSA); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
2016-01-25 01:26:53 -05:00
return jwt.PublicKey, nil
})
if err != nil {
return false
}
return tokenRequest.Valid
}
2016-01-26 15:08:45 -05:00
// DiskInfo - get disk statistics.
func (web *WebAPI) DiskInfo(r *http.Request, args *DiskInfoArgs, reply *disk.Info) error {
if !isAuthenticated(r) {
return errUnAuthorizedRequest
}
info, err := disk.GetInfo(web.FSPath)
if err != nil {
return err
}
*reply = info
return nil
}
2016-01-25 01:26:53 -05:00
// MakeBucket - make a bucket.
func (web *WebAPI) MakeBucket(r *http.Request, args *MakeBucketArgs, reply *string) error {
if !isAuthenticated(r) {
return errUnAuthorizedRequest
}
return web.Client.MakeBucket(args.BucketName, "", "")
}
// ListBuckets - list buckets api.
2016-01-25 01:26:53 -05:00
func (web *WebAPI) ListBuckets(r *http.Request, args *ListBucketsArgs, reply *[]BucketInfo) error {
if !isAuthenticated(r) {
return errUnAuthorizedRequest
}
2016-01-25 01:26:53 -05:00
buckets, err := web.Client.ListBuckets()
if err != nil {
return err
}
2016-01-25 01:26:53 -05:00
for _, bucket := range buckets {
*reply = append(*reply, BucketInfo{
Name: bucket.Name,
CreationDate: bucket.CreationDate,
})
}
return nil
}
// ListObjects - list objects api.
2016-01-25 01:26:53 -05:00
func (web *WebAPI) ListObjects(r *http.Request, args *ListObjectsArgs, reply *[]ObjectInfo) error {
if !isAuthenticated(r) {
return errUnAuthorizedRequest
}
doneCh := make(chan struct{})
defer close(doneCh)
2016-01-25 01:26:53 -05:00
for object := range web.Client.ListObjects(args.BucketName, args.Prefix, false, doneCh) {
if object.Err != nil {
return object.Err
}
2016-01-25 01:26:53 -05:00
*reply = append(*reply, ObjectInfo{
Key: object.Key,
LastModified: object.LastModified,
Size: object.Size,
})
}
return nil
}
// GetObjectURL - get object url.
func (web *WebAPI) GetObjectURL(r *http.Request, args *GetObjectURLArgs, reply *string) error {
if !isAuthenticated(r) {
return errUnAuthorizedRequest
}
2016-01-25 01:26:53 -05:00
urlStr, err := web.Client.PresignedGetObject(args.BucketName, args.ObjectName, time.Duration(60*60)*time.Second)
if err != nil {
return err
}
*reply = urlStr
return nil
}
// Login - user login handler.
func (web *WebAPI) Login(r *http.Request, args *LoginArgs, reply *AuthToken) error {
2016-01-25 01:26:53 -05:00
jwt := InitJWT()
if jwt.Authenticate(args.Username, args.Password) {
2016-01-25 01:26:53 -05:00
token, err := jwt.GenerateToken(args.Username)
if err != nil {
return err
}
reply.Token = token
return nil
}
return errUnAuthorizedRequest
}
// RefreshToken - refresh token handler.
func (web *WebAPI) RefreshToken(r *http.Request, args *LoginArgs, reply *AuthToken) error {
if isAuthenticated(r) {
2016-01-25 01:26:53 -05:00
jwt := InitJWT()
token, err := jwt.GenerateToken(args.Username)
if err != nil {
return err
}
reply.Token = token
return nil
}
return errUnAuthorizedRequest
}
// Logout - user logout.
func (web *WebAPI) Logout(r *http.Request, arg *string, reply *string) error {
if isAuthenticated(r) {
return nil
}
return errUnAuthorizedRequest
}