Skip to content
user_authentication.go 2.29 KiB
Newer Older
Simon Pastor's avatar
Simon Pastor committed
/*
 * Copyright (c) 2020  InterDigital Communications, 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.
 *
 * AdvantEDGE Platform Controller REST API
 *
 * This API is the main Platform Controller API for scenario configuration & sandbox management <p>**Micro-service**<br>[meep-pfm-ctrl](https://github.com/InterDigitalInc/AdvantEDGE/tree/master/go-apps/meep-platform-ctrl) <p>**Type & Usage**<br>Platform main interface used by controller software to configure scenarios and manage sandboxes in the AdvantEDGE platform <p>**Details**<br>API details available at _your-AdvantEDGE-ip-address/api_
 *
 * API version: 1.0.0
 * Contact: AdvantEDGE@InterDigital.com
 * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
 */

package server

import (
	"net/http"

	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
Kevin Di Lallo's avatar
Kevin Di Lallo committed
	sm "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-sessions-manager"
Simon Pastor's avatar
Simon Pastor committed
)

func init() {
	log.Info("Initializing User Auth.")

Kevin Di Lallo's avatar
Kevin Di Lallo committed
	_ = sm.Init("")
Simon Pastor's avatar
Simon Pastor committed

}

func uaLoginUser(w http.ResponseWriter, r *http.Request) {
	log.Info("----- LOGIN -----")

	// Get form data
	username := r.FormValue("username")
	password := r.FormValue("password")

Kevin Di Lallo's avatar
Kevin Di Lallo committed
	err := sm.AuthenticateNewUser(username, password, w, r)
Simon Pastor's avatar
Simon Pastor committed
	if err != nil {
Kevin Di Lallo's avatar
Kevin Di Lallo committed
		log.Error("There was an error during authentication of user ", username)
Simon Pastor's avatar
Simon Pastor committed
		return
	}

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusOK)
}

func uaLogoutUser(w http.ResponseWriter, r *http.Request) {
	log.Info("----- LOGOUT -----")
	//PrintConnectedUsers()
	// Get session cookie
Kevin Di Lallo's avatar
Kevin Di Lallo committed
	err := sm.AuthenticateUserDeletion(w, r)
Simon Pastor's avatar
Simon Pastor committed
	if err != nil {
Kevin Di Lallo's avatar
Kevin Di Lallo committed
		log.Error("There was an error during authentication of user")
Simon Pastor's avatar
Simon Pastor committed
		return
	}

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusOK)
}