<commit_before>package requesthandler

import (
	"log"

	"github.com/chandanchowdhury/HSPC/dbhandler"
	"github.com/chandanchowdhury/HSPC/restapi/operations"
	"github.com/chandanchowdhury/HSPC/restapi/operations/address"
	"github.com/chandanchowdhury/HSPC/restapi/operations/advisor"
	"github.com/chandanchowdhury/HSPC/restapi/operations/credential"
	"github.com/chandanchowdhury/HSPC/restapi/operations/login"
	"github.com/chandanchowdhury/HSPC/restapi/operations/problem"
	"github.com/chandanchowdhury/HSPC/restapi/operations/school"
	"github.com/chandanchowdhury/HSPC/restapi/operations/solution"
	"github.com/chandanchowdhury/HSPC/restapi/operations/student"
	"github.com/chandanchowdhury/HSPC/restapi/operations/team"
	"github.com/go-openapi/errors"
	"github.com/go-openapi/runtime/middleware"
)

/**
This function when called inside configureAPI() function in
 configure_hspc.go will override all NotImplemented calls with actual
 valid calls to handle the request.

Just add below line before return in configureAPI()

requesthandler.Override_configure_hspc(api)
*/
//
func Override_configure_hspc(api *operations.HspcAPI) {

	// Applies when the Authorization header is set with the Basic scheme
	api.UserSecurityBasicAuth = func(user string, pass string) (interface{}, error) {
		log.Print("Authenticating User = %s", user)

		credential := dbhandler.CredentialRead(user)

		log.Printf("From DB - email: %s, password: %s Active: %t", credential.Emailaddress.String(), credential.Password.String(), *credential.CredentialActive)

		if credential.Password.String() == <PASSWORD> && *credential.CredentialActive == true {
			log.Print("Password Matched")
			return user, nil
		}

		return nil, errors.Unauthenticated("api.hspc.ksu.edu")
	}

	// Applies when the Authorization header is set with the Basic scheme
	api.AdminSecurityBasicAuth = func(user string, pass string) (interface{}, error) {
		log.Print("Authenticating Admin = %s", user)

		//TODO: improvmenet required
		if user == "hspc" && pass == "<PASSWORD>" {
			return "HSPC", nil
		}

		return nil, errors.Unauthenticated("api.hspc.cs.ksu.edu")
	}

	api.AddressDeleteAddressIDHandler = address.DeleteAddressIDHandlerFunc(func(params address.DeleteAddressIDParams, principal interface{}) middleware.Responder {
		return HandleAddressDelete(params, principal)
	})
	api.AdvisorDeleteAdvisorIDHandler = advisor.DeleteAdvisorIDHandlerFunc(func(params advisor.DeleteAdvisorIDParams, principal interface{}) middleware.Responder {
		return HandleAdvisorDelete(params)
	})
	api.CredentialDeleteCredentialEmailaddressHandler = credential.DeleteCredentialEmailaddressHandlerFunc(func(params credential.DeleteCredentialEmailaddressParams, principal interface{}) middleware.Responder {
		return HandleCredentialDelete(params)<commit_msg>handle situation when admin tries to access user API<commit_after>package requesthandler

import (
	"log"

	"github.com/chandanchowdhury/HSPC/dbhandler"
	"github.com/chandanchowdhury/HSPC/restapi/operations"
	"github.com/chandanchowdhury/HSPC/restapi/operations/address"
	"github.com/chandanchowdhury/HSPC/restapi/operations/advisor"
	"github.com/chandanchowdhury/HSPC/restapi/operations/credential"
	"github.com/chandanchowdhury/HSPC/restapi/operations/login"
	"github.com/chandanchowdhury/HSPC/restapi/operations/problem"
	"github.com/chandanchowdhury/HSPC/restapi/operations/school"
	"github.com/chandanchowdhury/HSPC/restapi/operations/solution"
	"github.com/chandanchowdhury/HSPC/restapi/operations/student"
	"github.com/chandanchowdhury/HSPC/restapi/operations/team"
	"github.com/go-openapi/errors"
	"github.com/go-openapi/runtime/middleware"
)

/**
This function when called inside configureAPI() function in
 configure_hspc.go will override all NotImplemented calls with actual
 valid calls to handle the request.

Just add below line before return in configureAPI()

requesthandler.Override_configure_hspc(api)
*/
//
func Override_configure_hspc(api *operations.HspcAPI) {

	// Applies when the Authorization header is set with the Basic scheme
	api.UserSecurityBasicAuth = func(user string, pass string) (interface{}, error) {
		log.Print("Authenticating User = %s", user)

		credential := dbhandler.CredentialRead(user)

		if credential.CredentialID == 0 {
			return nil, errors.Unauthenticated("api.hspc.ksu.edu")
		}

		log.Printf("From DB - email: %s, password: %s Active: %t", credential.Emailaddress.String(), credential.Password.String(), *credential.CredentialActive)

		if credential.Password.String() == pass && *credential.CredentialActive == true {
			log.Print("Password Matched")
			return user, nil
		}

		return nil, errors.Unauthenticated("api.hspc.ksu.edu")
	}

	// Applies when the Authorization header is set with the Basic scheme
	api.AdminSecurityBasicAuth = func(user string, pass string) (interface{}, error) {
		//TODO: improvement required
		log.Printf("Authenticating Admin = %s", user)

		if user != "hspc" {
			return nil, errors.Unauthenticated("admin level access")
		}

		if user == "hspc" && pass == "<PASSWORD>" {
			return "HSPC", nil
		}

		return nil, errors.Unauthenticated("api.hspc.cs.ksu.edu")
	}

	api.AddressDeleteAddressIDHandler = address.DeleteAddressIDHandlerFunc(func(params address.DeleteAddressIDParams, principal interface{}) middleware.Responder {
		return HandleAddressDelete(params, principal)
	})
	api.AdvisorDeleteAdvisorIDHandler = advisor.DeleteAdvisorIDHandlerFunc(func(params advisor.DeleteAdvisorIDParams, principal interface{}) middleware.Responder {
		return HandleAdvisorDelete(params)
	})
	api.CredentialDeleteCredentialEmailaddressHandler = credential.DeleteCredentialEmailaddressHandlerFunc(func(params credential.DeleteCredentialEmailaddressParams, principal interface{}) middleware.Responder {
		return HandleCredentialDelete(params)
{'fraction_non_alphanumeric': 0.11061337008959339, 'fraction_numerical': 0.00017229496898690558, 'mean_word_length': 5.8861209964412815, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4999746', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1838, 'n_words': 341}