github.com/greenpau/go-authcrunch@v1.1.4/pkg/authn/handle_json_login.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package authn 16 17 import ( 18 "context" 19 "encoding/json" 20 "github.com/greenpau/go-authcrunch/pkg/requests" 21 "net/http" 22 ) 23 24 // AuthRequest is authentication request. 25 type AuthRequest struct { 26 Username string `json:"username,omitempty" xml:"username,omitempty" yaml:"username,omitempty"` 27 Password string `json:"password,omitempty" xml:"password,omitempty" yaml:"password,omitempty"` 28 Realm string `json:"realm,omitempty" xml:"realm,omitempty" yaml:"realm,omitempty"` 29 } 30 31 // AuthResponse is the response to authentication request. 32 type AuthResponse struct { 33 Token string `json:"token,omitempty" xml:"token,omitempty" yaml:"token,omitempty"` 34 TokenName string `json:"token_name,omitempty" xml:"token_name,omitempty" yaml:"token_name,omitempty"` 35 } 36 37 func (p *Portal) handleJSONLogin(ctx context.Context, w http.ResponseWriter, r *http.Request, rr *requests.Request) error { 38 authRequest := &AuthRequest{} 39 if r.Method != "POST" { 40 return p.handleJSONError(ctx, w, http.StatusUnauthorized, "Authentication Required") 41 } 42 r.Body = http.MaxBytesReader(w, r.Body, 1024) 43 respDecoder := json.NewDecoder(r.Body) 44 respDecoder.DisallowUnknownFields() 45 if err := respDecoder.Decode(authRequest); err != nil { 46 return p.handleJSONErrorWithLog(ctx, w, r, rr, http.StatusBadRequest, err.Error()) 47 } 48 49 rr.Response.Workflow = "json-api" 50 credentials := map[string]string{ 51 "username": authRequest.Username, 52 "password": authRequest.Password, 53 "realm": authRequest.Realm, 54 } 55 56 if err := p.authenticateLoginRequest(ctx, w, r, rr, credentials); err != nil { 57 return p.handleJSONErrorWithLog(ctx, w, r, rr, rr.Response.Code, err.Error()) 58 } 59 if err := p.authorizeLoginRequest(ctx, w, r, rr); err != nil { 60 return p.handleJSONErrorWithLog(ctx, w, r, rr, rr.Response.Code, err.Error()) 61 } 62 usr, err := p.sessions.Get(rr.Upstream.SessionID) 63 if err != nil { 64 return p.handleJSONErrorWithLog(ctx, w, r, rr, http.StatusInternalServerError, err.Error()) 65 } 66 resp := &AuthResponse{ 67 TokenName: usr.TokenName, 68 Token: usr.Token, 69 } 70 respBytes, _ := json.Marshal(resp) 71 w.WriteHeader(rr.Response.Code) 72 w.Write(respBytes) 73 return nil 74 }