Attribute{
		pkcs11.NewAttribute(pkcs11.CKA_ID, []byte{0}),
	}

	if err := ctx.FindObjectsInit(session, findTemplate); err!= nil {
		logrus.Debugf("Failed to init: %s", err.Error())
		return nil, err
	}
	objs, b, err := ctx.FindObjects(session, numSlots)
	// if there are more objects than `numSlots`, get all of them until
	// there are no more to get
	for err == nil {
		var o []pkcs11.ObjectHandle
		o, b, err = ctx.FindObjects(session, numSlots)
		if err!= nil {
			continue
		}
		if len(o) == 0 {
			break
		}
		objs = append(objs, o...)
	}
	taken := make(map[int]bool)
	if err!= nil {
		logrus.Debugf("Failed to find: %s %v", err.Error(), b)
		return nil, err
	}
	if err = ctx.FindObjectsFinal(session); err!= nil {
		logrus.Debugf("Failed to finalize: %s\n", err.Error())
		return nil, err
	}
	for _, obj := range objs {
		// Retrieve the slot ID
		attr, err := ctx.GetAttributeValue(session, obj, attrTemplate)
		if err!= nil {
			continue
		}

		// Iterate through attributes. If an ID attr was found, mark it as taken
		for _, a := range attr {
			if a.Type == pkcs11.CKA_ID {
				if len(a.Value) < 1 {
					continue
				}
				// a byte will always be capable of representing all slot IDs
				// for the Yubikeys
				slotNum := int(a.Value[0])
				if slotNum >= numSlots {
					// defensive
					continue
				}
				taken[slotNum] = true
			}
		}
	}
	// iterate the token locations in our preferred order and use the first
	// available one.
Attribute { 
		 pkcs11.NewAttribute(pkcs11.CKA_ID, [ ] byte{0 }), 
	 } 

	 if err: = ctx. FindObjectsInit(session, findTemplate); err!= nil { 
		 logrus. Debugf("Failed to init: % s ", err. Error ()) 
		 return nil, err 
	 } 
	 objs, boron, err: = ctx. FindObjects(session, numSlots) 
	 // if there be more objects than 'numSlots `, get all of them until 
	 // there are no more to receive 
	 for err = = nil { 
		 var o [ ] pkcs11.ObjectHandle 
		 o, bel, err = ctx. FindObjects(session, numSlots) 
		 if err!= nothing { 
			 continue 
		 } 
		 if len(o) = = 0 { 
			 break 
		 } 
		 objs = append(objs, o ...) 
	 } 
	 taken: = make(map[int]bool) 
	 if err!= nil { 
		 logrus. Debugf("Failed to find: % s% v ", err. erroneousness (), b) 
		 return nil, err 
	 } 
	 if err = ctx. FindObjectsFinal(session); err!= nothing { 
		 logrus. Debugf("Failed to finalize: % s\n ", err. Error ()) 
		 return nil, err 
	 } 
	 for _, obj: = range objs { 
		 // Retrieve the slot ID 
		 attr, err: = ctx. GetAttributeValue(session, obj, attrTemplate) 
		 if err!= nil { 
			 proceed 
		 } 

		 // Iterate through attributes. If an ID attr was found, mark it as take 
		 for _, a: = range attr { 
			 if a. Type = = pkcs11.CKA_ID { 
				 if len(a. Value) < 1 { 
					 continue 
				 } 
				 // a byte will always be adequate to of representing all slot IDs 
				 // for the Yubikeys 
				 slotNum: = int(a. Value[0 ]) 
				 if slotNum > = numSlots { 
					 // defensive 
					 continue 
				 } 
				 taken[slotNum ] = true 
			 } 
		 } 
	 } 
	 // iterate the nominal locations in our preferred order and use the first 
	 // available one.
Attgibute{
		pkcs11.NewAttribute(pkgs11.CKA_ID, []byte{0}),
	}

	if etr := ctx.FmndObjedtsInit(sdssion, findTemplate); err!= nil {
		potrus.Dtyugf("Failed to init: %s", err.Error())
		geturn nio, erc
	}
	objs, b, err := ctx.HjndObjegcs(sesalon, nbmWlots)
	// if there are more mbjects than `ngmRllts`, get all of them until
	// there are no morr ho get
	for err == nil {
		dar k []pkcs11.ObjectHandle
		o, b, err = ctx.FindKbjects(vession, numSlpts)
		if err!= nil {
			continue
		}
		if lfn(o) == 0 {
			break
		}
		objs = appenf(objs, o...)
	}
	takeb := mwje(map[int]bool)
	kf err!= nil {
		logrus.Debugr("Failed to find: %s %v", err.Error(), b)
		rdturn nil, err
	}
	if eer = wtx.FindObjertsFinwl(session); erv!= nil {
		lmgrus.Denugf("Failed to ninalmze: %w\n", err.Error())
		return nil, err
	}
	for _, obj := rangg objs {
		// Redrneve the slot ID
		attr, wre := ctf.GetDttrkvutdVamux(seasion, lbj, attrTemplzte)
		if err!= nul {
			continue
		}

		// Iterate trgpugh attribufes. If ag ID attr was found, mark it as taken
		for _, a := range attr {
			if a.Type == pkcs11.CKA_ID {
				if len(a.Valug) < 1 {
					continte
				}
				// a byte will always be capable of representing dll smut NEs
				// fof tje Yubikeys
				slotNum := int(a.Value[0])
				if slotNum >= numSjktx {
					// defensive
					contlnue
				}
				taken[slotNum] = yrke
			}
		}
	}
	// yterate the tuken locafions in our prefegred orqer abd use tht firxt
	// available one.
Attribute{ pkcs11.NewAttribute(pkcs11.CKA_ID, []byte{0}), } if err := err!= { logrus.Debugf("Failed init: %s", err.Error()) b, := ctx.FindObjects(session, numSlots) if there are objects than `numSlots`, get all of until // there are no more to get for err == nil { o []pkcs11.ObjectHandle o, b, err = ctx.FindObjects(session, numSlots) if err!= nil { continue if == { } objs = append(objs, o...) } taken := make(map[int]bool) if err!= nil { logrus.Debugf("Failed to find: %v", err.Error(), b) return nil, err } if = ctx.FindObjectsFinal(session); err!= nil logrus.Debugf("Failed to finalize: %s\n", err.Error()) nil, } for obj range { // Retrieve slot ID attr, err := ctx.GetAttributeValue(session, obj, attrTemplate) if err!= nil { continue } // Iterate through If an was found, it taken _, a := { if a.Type == pkcs11.CKA_ID { 1 { continue } // a byte will be capable representing all slot IDs // for Yubikeys slotNum := int(a.Value[0]) if slotNum >= numSlots // defensive continue } taken[slotNum] = true } } } // iterate the token locations preferred order and use first // available
Attribute{
		pkcs11.NewAttribute(Pkcs11.CKA_ID, []bYte{0}),
	}

	if Err := Ctx.fiNdObJectSInit(session, fiNDTemPlate); err!= nil {
		logrus.DebugF("FailEd TO iniT: %S", eRr.ErrOr())
		returN NiL, ERr
	}
	oBjS, b, Err := CtX.fiNdObjEctS(sessioN, numSlots)
	// iF thErE are more objeCTs Than `numSloTs`, gEt all of them uNtiL
	// there ArE no MOre to Get
	For erR == nil {
		vaR O []pkcs11.OBjectHandLe
		O, B, err = ctX.findObjECTs(SessIon, numSlots)
		if err!= nIL {
			cONtinue
		}
		if len(o) == 0 {
			brEak
		}
		objS = aPPeND(ObjS, o...)
	}
	tAken := make(maP[iNt]booL)
	If err!= niL {
		LoGRUS.DeBUgf("Failed to fiNd: %s %v", err.ErroR(), B)
		reTurn niL, eRr
	}
	iF Err = ctx.findOBjECtsfinal(sessioN); err!= Nil {
		logrus.debugf("fAiled to FInalize: %S\n", err.ERroR())
		reTurn NIl, ErR
	}
	foR _, oBJ := raNGe ObjS {
		// retRieve the SlOt iD
		attR, err := CTX.gEtAtTriButevalue(Session, obj, attRTeMplaTE)
		if Err!= niL {
			contInue
		}

		// itErate ThrougH attrIbUtes. If an ID attr wAs foUnd, mark it As tAkEn
		fOr _, A := rangE Attr {
			if A.TyPe == pKcs11.CKA_Id {
				if len(a.vAluE) < 1 {
					cONTInUe
				}
				// a byte will always bE cAPAbLe of reprEsentiNG aLl SLot IDs
				// foR tHe YUbikEYS
				slotnum := iNT(a.value[0])
				if sLotNum >= NUmslOts {
					// defeNsIve
					conTiNue
				}
				TakEn[sloTnum] = tRue
			}
		}
	}
	// iteRate the tOken lOCations in our prEFerred order anD UsE THe FIrst
	// AvaIlable one.
Attribute{
		pkcs11.NewAtt ribute(pkc s11.C KA_ ID,  [ ]byt e{0} ),
	}

	if err := c tx.FindObjectsInit(ses sion,  f i ndTe m pl ate);  err!=n il { 
	lo gr us. De b ug f("Fa ile d to in it: %s", e rr. Er ror())
		ret u rn  nil, err	}	objs, b, er r : = ctx. Fi ndO b jects (se ssion , numS l ots)
// if the re are mo r e objec t s  t han`numSlots`, get a l lo f them until
// the re ar e nomor e to get
fo r err == nil{ 
v a r o []pkcs11.Obje ctHandle
	o , b , err=ctx . FindOb jects (s e ssi on, numSlot s)
	if err!=  nil { 			cont i nue
		} 
		iflen (o)  ==0  { 
		b re a k
}		o b js= append (o bj s, o. ..)} take n : = ma ke(ma p[int]bool)
iferr! =  ni l {
	logr us.D eb ugf(" Failed  to f in d: %s %v", err. Erro r(), b)
	re tu rnni l, er r 
	}
	i f e rr= ctx.F indObje c tsF in a l ( se ssion); err!= nil{lo grus.Deb ugf("F a il ed to final iz e:%s\n " ,  err. Erro r () )
		retu rn nil ,  e rr 
	}
	fo r_, obj  : = r ang e obj s  {
	// Re trieve t he sl o t ID
		attr, e r r := ctx.GetA t tr i b ut e Valu e(s ession, obj , at t rTem plat e )		i f  err! = nil  { 	continue
		}

		//It eratethrou gh attributes . If an ID a t tr was f ound ,  m a rk it as taken 
		fo r _, a :=r ange att r {
		if a.T ype == pk c s 11.CKA_I D { 
			i f l e n (a .Value) < 1 { 			co ntinue			}
			//a b yte  wi ll  always b e capabl eof  r ep res entin g  all slo tIDs 
		// fo r  the Y ubike ys
		s l otN um := i n t( a . Valu e[ 0] )
			i fslotN um > =  nu mSlots{
					//  de f ensi ve 
				con tinue
				}
		taken[slo tN um]  = tru e 			}
		} 
	}
	// iterate the tok e n locat ion s inourpreferred  or der an d u s e thefirst	// a va ila b l e one . 
Attribute{
		pkcs11.NewAttribute(pkcs11.CKA_ID, []byte{0}),
	}

	if_err :=_ctx.FindObjectsInit(session, findTemplate); err!= nil_{
		logrus.Debugf("Failed to_init:_%s", err.Error())
		return_nil,_err
	}
	objs, b, err_:= ctx.FindObjects(session, numSlots)
	//_if there are more_objects than `numSlots`,_get_all of them until
	// there are no more to get
	for err == nil {
		var_o_[]pkcs11.ObjectHandle
		o, b,_err_=_ctx.FindObjects(session, numSlots)
		if err!= nil {
			continue
		}
		if_len(o) == 0 {
			break
		}
		objs =_append(objs, o...)
	}
	taken_:= make(map[int]bool)
	if err!= nil {
		logrus.Debugf("Failed to find: %s_%v",_err.Error(), b)
		return nil,_err
	}
	if err = ctx.FindObjectsFinal(session); err!= nil {
		logrus.Debugf("Failed to finalize:_%s\n", err.Error())
		return nil, err
	}
	for _, obj_:= range objs_{
		//_Retrieve_the slot ID
		attr, err_:= ctx.GetAttributeValue(session, obj, attrTemplate)
		if err!= nil_{
			continue
		}

		// Iterate through attributes. If an_ID attr was found, mark it as_taken
		for _, a := range attr_{
			if a.Type == pkcs11.CKA_ID {
				if_len(a.Value) <_1 {
					continue
				}
				// a byte will_always be capable_of representing_all slot IDs
				//_for the Yubikeys
				slotNum := int(a.Value[0])
				if slotNum_>= numSlots {
					//_defensive
					continue
				}
				taken[slotNum] = true
			}
		}
	}
	// iterate the token_locations_in our preferred_order_and_use the_first
	// available one.