Rules: How does right to castle and en passant get considered for the purpose of calculating a threefold repetition? I'm writing a chess app, and have some questions regarding the intricacies of the threefold repetition rules.

As of 2018, the FIDE handbook has this to say about what constitutes identical positions for the purpose of threefold repetitions.


  9.2.2  Positions are considered the same if and only if the same player has the move, pieces of the same kind and colour occupy the
  same squares and the possible moves of all the pieces of both players
  are the same. Thus positions are not the same if:
  
  9.2.2.1   at the start of the sequence a pawn could have been captured en passant
  
  9.2.2.2   a king had castling rights with a rook that has not been moved, but forfeited these after moving. The castling rights are lost
  only after the king or rook is moved.


This question and Rule 9.2.2.2 address calculating right to castle. Looking at both, my conclusion is that it only checks whether the relevant piece(s) have moved; other restrictions like check, obstructions, or even whether the relevant rook was captured are not considered (though the last is probably irrelevant). 

The same rule also seems to specify that each player starts with 2 castling rights- one for kingside and one for queenside- rather than 3- one for the king and one for each rook.

So the first part of my question is, am I correct in my assessment of how the right to castle affects whether positions are identical?

For the second part of my question, there seems to be a little ambiguity regarding en-passant.Rule 9.2.2.1 is clear about the case of if a pawn can legally en-passant one turn and can't during a later turn, then the positions aren't identical even if everything else is the same.

But there are two cases with en passant that aren't clear, or at least, should be clarified in light of how right to castle is calculated.

First, say a pawn moves two squares, thus making it capturable by en-passant, but there's no enemy pawn next to its destination. Does the fact that it would have been vulnerable to en passant make the position non-identical to another otherwise identical position a few turns later?

Second, say a pawn moves two squares, landing next to an enemy pawn, giving that pawn the right to en-passant, but where exercising the right to en-passant would expose the king (thus barring the otherwise legal en passant). Would that position be considered different from an otherwise identical position a few turns later?
You have identified the correct rule governing these situations. 9.2.2 is the relevant clause that applies; 9.2.2.1 and 9.2.2.2 are just clarifications of this rule.

So to answer your queries:

1) Your interpretation of castling rights is correct. Each player has two castling rights which are irrespective of the current board position. The right to castle short is lost only when the king or h-rook moves for the first time. The right to castle long is lost only when the king or a-rook moves for the first time. Other restrictions are irrelevant.

2) If a pawn moves two squares without an enemy pawn ready to capture en passant next to it, the position is the same as the one which could reoccur after some more moves. This is because all the players have the same possible (legal) moves (the relevant en passant is, was, and will be unavailable) so under 9.2.2 it would be the same position.

3) Similarly, if en passant is illegal due to a pin (exposing own king to check) or another reason, it does not count as a possible (legal) move, so under 9.2.2 the position and the repeated one later are considered the same.

An additional note: comparing FEN is not a correct implementation according to the FIDE rules, since FEN always records down a pawn moving two squares, regardless of whether an en passant capture is possible or not. Hence a direct PGN comparison would yield a wrong conclusion in the two scenarios you described.